关于#python#温度转化的问题:SyntaxError: unexpected EOF while parsing

#小白1.3实例练习python温度转换一直无法运行

#老师演示的代码如下

img

我写的如下:

#tempconvert.py
TempStr = input("temperature value with signs:")
if TempStr[-1] in ['F','f']:
    C = (eval(tempstr[0:-1]) - 32)/1.8
    print("the converted temperature is{:.2f}C".format(C))
elif TempStr[-1] in ['C','c']:
    F = 1.8*eval(tempstr[0:-1]) + 32
    print("the converted temperature is{:.2f}F".format(F))
else:
    print("filed to output")

因为反复修改还是运行不成功,不知道是不是输入法的问题,就把里面的一些描述改成全英文了,甚至还直接复制了他人写的代码试运行,也不行
然而报错一直如下

Python 2.7.16 (v2.7.16:413a49145e, Mar  4 2019, 01:30:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
===================== RESTART: D:/python/tempconvert.py =====================
temperature value with signs:85f

Traceback (most recent call last):
  File "D:/python/tempconvert.py", line 2, in <module>
    TempStr = input("temperature value with signs:")
  File "<string>", line 1
    85f
      ^
SyntaxError: unexpected EOF while parsing
>>> 

请求解答是什么问题

我这里测试将第4行和第7行的tempstr 改为TempStr,程序正常运行。

测试如下:



#tempconvert.py
TempStr = input("temperature value with signs:")
if TempStr[-1] in ['F','f']:
    # 将tempstr 改为TempStr
    C = (eval(TempStr[0:-1]) - 32)/1.8
    print("the converted temperature is {:.2f}C".format(C))
elif TempStr[-1] in ['C','c']:
    # 将tempstr 改为TempStr
    F = 1.8*eval(TempStr[0:-1]) + 32
    print("the converted temperature is {:.2f}F".format(F))
else:
    print("filed to output")
 


img

新建一个python文件,重新贴一下你的代码,去掉空格空行这些,再保存看看

代码语法并没有问题,但你的输入有问题

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^