我的代码将华氏温度转换为摄氏温度。就是这个。使用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
,但我不知道。任何帮助,将不胜感激!
请显示您收到的错误。 – cco