繁体   English   中英

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

[英]Using a Try and Except block, but in the Except Part it says Not Defined 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但我不知道。 任何帮助,将不胜感激!

当您执行fahrenheit - 32 ,您的代码尝试从字符串中减去整数,这将导致以下异常:

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

这不是ValueError ,所以它不会被您的except子句捕获。

如果要捕获ValueError ,则需要在您的try:块中放入fahrenheit = int(fahrenheit)

您正在使用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

使用python 3.5运行显示input()

"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'

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

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

希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM