我的代碼將華氏溫度轉換爲攝氏溫度。就是這個。使用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