I've written a simple temperature converter in Python, but I get some pretty weird errors. When I choose option f when running the program in the Python interpreter, and enter a temperature value I get this error:
Traceback (most recent call last):
File "temp_convert.py", line 23, in <module>
menu()
File "temp_convert.py", line 5, in menu
ctf()
File "temp_convert.py", line 13, in ctf
temp = (c * 1.8) + 32
TypeError: can't multiply sequence by non-int of type 'float'
I understand what this means but I don't know how to tell Python I need, or rather want, to use a float. I thought numerical values can change on the fly in pYthon and not cause problems.
As for when I choose option c when running the program, I get this error, which I also don't understand:
Traceback (most recent call last):
File "temp_convert.py", line 23, in <module>
menu()
File "temp_convert.py", line 7, in menu
ftc()
File "temp_convert.py", line 19, in ftc
temp = (f -32) / 1.8
TypeError: unsupported operand type(s) for -: 'str' and 'int'
Here is all my code below. Any help you can give is greatly appreciated. Thanks in advance!
def menu(): print "Press f to convert to Fahrenheit from Celsius, or c to convert to Celsius from Fahrenheit." choice=raw_input() if choice =="f": ctf() elif choice =="c": ftc() else: print "Invalid selection!" def ctf(): print "Enter the temperature to be converted to degrees Fahrenheit. from degrees Celsius." c=raw_input() temp = (c * 1.8) + 32 print "The temperature in degrees Fahrenheit is %s." % temp menu() def ftc(): print "Enter the temperature to be converted to degrees Celsius from degrees Fahrenheit." f=raw_input() temp = (f -32) / 1.8 print "The temperature in degrees Celsius is %s degrees" % temp menu() print "Welcome to the temperature converter!" menu()
This post has been edited by atraub: 10 June 2011 - 08:08 PM
Reason for edit:: Code tags ftw

New Topic/Question
Reply




MultiQuote










|