[Tutor] Re: Newbie - mixing floats and integers
Lee Harr
missive at hotmail.com
Sun Apr 11 19:22:32 EDT 2004
>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
>
>
>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?
What input? What errors?
Most likely, what you want to do is use float(arg) instead of long(arg)
long() is going to produce a long integer, which in python is an
integer that can be arbitrarily large (or anti-large).
Remember that you can run some of this stuff by the interactive
interpreter to see what it means ...
>>>3.5
3.5
>>>long(3.5)
3L
>>>long(-3.5)
-3L
>>>long(-39999999999999999999999999999999999999)
-39999999999999999999999999999999999999L
>>>float(35)
35.0
>>>float(35.5)
35.5
>>>float('thirty-five')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for float(): thirty-five
>>>float('35')
35.0
>>>float('35.5')
35.5
_________________________________________________________________
The new MSN 8: advanced junk mail protection and 2 months FREE*
http://join.msn.com/?page=features/junkmail
More information about the Tutor
mailing list