编写代码以转换温度:
#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 :(得分: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")