我在最后一行 - 第 14 行遇到语法错误。我不明白为什么,因为它似乎是一个简单的打印语句。
cel = "c"
far = "f"
cdegrees = 0
fdegrees = 0
temp_system = input ("Convert to Celsius or Fahrenheit?")
if temp_system == cel:
cdegrees = input ("How many degrees Fahrenheit to convert to Celsius?")
output = 5/9 * (fdegrees - 32)
print "That's " + output + " degrees Celsius!"
elif temp_system == far:
fdegrees = input ("How many degrees Celsius to convert to Fahrenheit?")
output = (32 - 5/9) / cdegrees
print "That's " + output + " degrees Fahrenheit!"
else print "I'm not following your banter old chap. Please try again."
请您参考如下方法:
您忘记了最后一个 else 之后的冒号 (:)。
还有:
input ("Convert to Celsius or Fahrenheit?")
应该改为
raw_input ("Convert to Celsius or Fahrenheit?")
因为 input() 尝试评估其输入,而 raw_input 接受“原始”字符串。例如,当您在 input() 中输入 c 时,它会尝试评估表达式 c ,就好像它是寻找变量的 python 代码一样c 而 raw_input 只是获取字符串而不尝试对其求值。
此外,您不能像在这种情况下那样将字符串与整数连接(加在一起),其中 output 是一个数字。
改成
print "That's " + str(output) + " degrees Celsius!"
或
print "That's %d degrees Celsius!" % output
