2017-08-25 78 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") 
相关问题