使用Try-and-Except块,但在Except部分,它表示未定义Python

使用Try-and-Except块,但在Except部分,它表示未定义Python,python,python-2.7,python-3.x,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("Wha

我的代码是把华氏温度转换成摄氏温度。就是这样

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(“您想将什么温度从华氏温度转换为摄氏温度?”)
,如何使用Try位将引号中的数字转换为不带引号的正常数字?我想这是因为我选择了
值错误
,但我不知道。任何帮助都将不胜感激

您使用的是python 2.x,而不是3.x。在Python2.x中,您需要使用
raw\u 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
"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'
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'
运行显示
原始输入()
使用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
"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'
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'

执行
华氏-32
时,代码试图从字符串中减去整数,这会导致此异常:

TypeError: unsupported operand type(s) for -: 'str' and 'int'
这不是
ValueError
,因此它不会被
except
子句捕获


如果要捕获
ValueError
,需要将
fahrenheit=int(fahrenheit)
放入
try:
块中。

将用户输入转换为整数,如下所示:

fahrenheit = int(fahrenheit)
如果您使用的是Python3.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()

希望有帮助

请显示您收到的错误。您的代码将生成ValueError,但如果传递一个字母,则不会捕获ValueError。它会生成ValueError。我只是检查一下。是的,是的,但不在
try
内,这样它就不会被抓到并打印“那不是一个数字”。