Zctrap
Zctrap

Reputation: 3

Using a Try and Except block, but in the Except Part it says Not Defined Python

My code is converting Fahrenheit to Celsius. This is it.

def again():
    calc = raw_input("Press y to convert again. Press n to quit. ").lower()
    if calc == "y":
        main()
    elif calc == "n":
        quit()
    else:
        again()

def main():
    fahrenheit = input("What do you want to convert from Fahrenheit to Celsius? ")
    try:
        celsius = (fahrenheit - 32) *5 / float(9) 
        print('%0.2F degree Celsius is equal to about %0.1f degree Fahrenheit' %(celsius,fahrenheit))
        again()
    except ValueError:
        print("That is not a number")
        check()

If you run it, and type a letter instead of a number to convert. It doesn't execute the Except bit but says that letter is not defined. If I did fahrenheit = input("What do you want to convert from Fahrenheit to Celsius? "), how would I make the Try bit to turn the number which is in quotes into a normal number without the quotes? I think it is because I chose Value Error but I have no idea. Any help would be appreciated!

Upvotes: 0

Views: 260

Answers (3)

vacky
vacky

Reputation: 287

Convert the input from user into an integer in try as follows:

fahrenheit = int(fahrenheit)

if you are using python 3.x, this should be your final code

def again():
calc = input("Press y to convert again. Press n to quit. ").lower()
if calc == "y":
    main()
elif calc == "n":
    quit()
else:
    again()

def main():
fahrenheit = input("What do you want to convert from Fahrenheit to Celsius? ")
try:
    fahrenheit = int(fahrenheit)
    celsius = (fahrenheit - 32) *5 / float(9) 
    print('%0.2F degree Celsius is equal to about %0.1f degree Fahrenheit' %(celsius,fahrenheit))
    again()
except ValueError:
    print("That is not a number")
    #check()
main()

hope it helps

Upvotes: 0

cco
cco

Reputation: 6291

Your code attempts to subtract an integer from a string when you execute fahrenheit - 32, which causes this exception:

TypeError: unsupported operand type(s) for -: 'str' and 'int'

which isn't a ValueError, so it won't get caught by your except clause.

If you want to catch a ValueError, you need to put fahrenheit = int(fahrenheit) inside your try: block.

Upvotes: 1

Stephen Rauch
Stephen Rauch

Reputation: 49814

You are using python 2.x, not 3.x. In python 2.x you need to use raw_input, not input. So change this:

fahrenheit = input(
    "What do you want to convert from Fahrenheit to Celsius? ")

to this:

fahrenheit = raw_input(
    "What do you want to convert from Fahrenheit to Celsius? ")

Run showing input() failing in python 2.7:

C:\Python27\python.exe test.py
What do you want to convert from Fahrenheit to Celsius? d
Traceback (most recent call last):
  ...
    "What do you want to convert from Fahrenheit to Celsius? ")
  File "<string>", line 1, in <module>
NameError: name 'd' is not defined

Run showing input() working with python 3.5:

"C:\Program Files (x86)\Python35-32\python.exe" test.py
What do you want to convert from Fahrenheit to Celsius? d
Traceback (most recent call last):
  ...
  File "C:/Users/stephen/Documents/src/testcode/test.py", line 31, in main
    celsius = (fahrenheit - 32) * 5 / float(9)
TypeError: unsupported operand type(s) for -: 'str' and 'int'

Run showing raw_input() working with python 2.7:

C:\Python27\python.exe test.py awk_data.txt
What do you want to convert from Fahrenheit to Celsius? d
Traceback (most recent call last):
...
  File "C:/Users/stephen/Documents/src/testcode/test.py", line 31, in main
    celsius = (fahrenheit - 32) * 5 / float(9)
TypeError: unsupported operand type(s) for -: 'str' and 'int'

Upvotes: 0

Related Questions