[Tutor] Newbie - mixing floats and integers (first post)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Apr 12 14:45:54 EDT 2004

On Sun, 11 Apr 2004, Adam wrote:

> Just picking up python with Learning Python (on day 3 now - be kind) and
> have written my own small calculator script:
> arg1 = raw_input("What is your first number?:")
> oper = raw_input("What operation: +, -, *, / ?")
> arg3 = raw_input("What is your second number?:")
> num1 = long(arg1)
> #oper = arg [2]
> num2 = long(arg3)
> if oper == "+":
>         answer = num1+num2
> elif oper == "-":
>         answer = num1-num2
> elif oper == "/":
>         answer = (num1/num2)
> elif oper == "x":
>         answer = (num1*num2)
> print num1, oper, num2, "=",  answer
> I've read in learning Python that the variable types look after
> themselves - "great" I think, less hassle than Java.
> However, when I try and run this program with floats, it won't work and
> I get errors. Is there something I have to do to enable this to work
> with both floats and integers?

Hi Adam,

Yes, there's a way to do it.

(By the way, instead of using long(), use int().  In Python 2.3, int()
will automagically use long() if the integer is larger than your machine's
native integer size:

>>> int("323984729834729374293892749327894273984723895")

so unless you're trying to maintain compatibility with older versions of
Python, you don't have to worry about overflowing.)

The issue is the conversion of the 'arg1' and 'arg3' strings to some kind
of number.  At the moment, the code is indiscriminately trying to convert
everything to long integers.  But when we try feeding in a string that
looks like a float, though, we'll get exceptions that warn us that we're
probably doing something silly:

>>> long("3.1415")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid literal for long(): 3.1415

We can, however, convert a string that looks like a float directly into a

>>> float("3.1415")

One way to fix the program is to call either int() or float(),
conditionally based on the content of the string.  Here's one approach: if
the string contains a decimal point, we can call our float conversion
routine.  And otherwise, we can call our integer-converting routine:

>>> def num(s):
...     """Returns either an integer or a float out of string 's'."""
...     if '.' in s:
...         return float(s)
...     else:
...         return int(s)
>>> num("3.1415926")
>>> num("42")

You can use this 'num()' function to do the string-to-numeric conversions,
and it should do the right thing, for the most part.  *grin*

The problem is very similar to what happens in Java when we use Java's
Integer.parseInt() function on a string that looks like a floating-point

[dyoo at tesuque dyoo]$ jython
Jython 2.1 on java1.4.1_01 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> import java
>>> java.lang.Integer.parseInt("3.1415926")
Traceback (innermost last):
  File "<console>", line 1, in ?
java.lang.NumberFormatException: For input string: "3.1415926"

[... long Java traceback cut]

(I'm using Jython --- a Java/Python bridge --- to show what things look
like on the Java side.  It's a very nice tool if you have Java

So the conversion issues between strings and the primitive types are also
similar in Java --- the string-to-integer routines on both are a bit

Please feel free to ask more questions.  Hope this helps!

More information about the Tutor mailing list