2017-02-19 27 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