使用Try和Except块,但在Except Part中它表示Not Defined Python

时间:2017-02-19 05:30:47

标签: python python-2.7 python-3.x

我的代码正在将华氏温度转换为摄氏温度。就是这样。

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但我不知道。任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:1)

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

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

不是ValueError,因此不会被except条款捕获。

如果您想抓住ValueError,则需要将fahrenheit = int(fahrenheit)放在try:区块内。

答案 1 :(得分: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? ")

在python 2.7中运行显示input()失败:

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'

答案 2 :(得分:0)

将try中的输入转换为try中的整数,如下所示:

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

希望有所帮助