2017-08-25 51 views
-1

編寫代碼轉換溫度:語法錯誤:意外的EOF在解析中Python2.7

#TempConvert.py 
val = input("Input Temperature(eg. 32C): ") 
if val[-1] in ['C','c']: 
    f = 1.8 * float(val[0:-1]) + 32 
    print("Converted Temperture : %.2fF"%f) 
elif val[-1] in ['F','f']: 
    c = (float(val[0:-1]) - 32)/1.8 
    print("Converted Temperture: %.2fC"%c) 
else: 
    print("Input Error") 

當乳寧在Python2.7的代碼,得到一個錯誤:

enter code ============= RESTART: D:\workshop_for_Python\TempConvert -2.py ============= 
Input Temperture(eg. 32C): 33C 

Traceback (most recent call last): 
    File "D:\workshop_for_Python\TempConvert -2.py", line 2, in <module> 
    val = input("Input Temperture(eg. 32C): ") 
    File "<string>", line 1 
    33C 
    ^
SyntaxError: unexpected EOF while parsinghere 

任何想法什麼這個問題?多謝〜

+0

的可能的複製[輸入()錯誤 - NameError:不定義名稱「...」(https://stackoverflow.com/questions/21122540 /輸入錯誤nameerror-NAME是-不定義) –

回答

2

錯誤的來源是使用input()作爲輸入,因爲它只允許讀取整數值。因此,在代碼的唯一修改將使用raw_input()

#TempConvert.py 
val = raw_input("Input Temperature(eg. 32C): ") 
if val[-1] in ['C','c']: 
    f = 1.8 * float(val[0:-1]) + 32 
    print("Converted Temperture : %.2fF"%f) 
elif val[-1] in ['F','f']: 
    c = (float(val[0:-1]) - 32)/1.8 
    print("Converted Temperture: %.2fC"%c) 
else: 
    print("Input Error")