2017-02-19 29 views
0

我的代码将华氏温度转换为摄氏温度。就是这个。使用Try和Except块,但在Except部分中,它表示未定义Python

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() 

如果您运行它,并键入一个字母而不是数字进行转换。它不执行Except位,但表示该字母未被定义。如果我做了 fahrenheit = input("What do you want to convert from Fahrenheit to Celsius? "),我该如何让Try位将引号中的数字转换为不带引号的正常数字?我认为这是因为我选择了Value Error,但我不知道。任何帮助,将不胜感激!

+0

请显示您收到的错误。 – cco

回答

1

您的代码时,尝试执行fahrenheit - 32,这导致该异常从字符串减去整数:

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

这是不是ValueError,所以它不会被你的except条款捕获。

如果你想赶上ValueError,你需要把fahrenheit = int(fahrenheit)放在你的try:块内。

0

您使用的是Python 2.x,而不是3.x.在python 2.x中,你需要使用raw_input而不是input。因此,改变这种:

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

这样:

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

预演input()在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 

预演input()使用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' 

预演raw_input()使用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' 
0

转换从用户输入到一个整数尝试如下:

fahrenheit = int(fahrenheit) 

如果你使用python 3.x中,这应该是你的最终代码

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() 

希望它有助于

+0

您的代码将生成,但如果传递一封信,则不会捕获ValueError。 – cco

+0

它确实生成ValueError。我只是检查它。 – vacky

+0

是的,它确实存在,但不在'try'里面,所以它不会被抓到并且打印出“那不是数字”。 – cco