我在最后一行 - 第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."
Run Code Online (Sandbox Code Playgroud)
你忘:了最后一个冒号()else.
也:
input ("Convert to Celsius or Fahrenheit?")
Run Code Online (Sandbox Code Playgroud)
应改为
raw_input ("Convert to Celsius or Fahrenheit?")
Run Code Online (Sandbox Code Playgroud)
作为input()同时试图评估其输入raw_input需要一个"原始"的字符串.c例如,当你输入input()它时,它试图评估表达式c,好像它是查找变量的python代码,c而raw_input只是接受字符串而不试图评估它.
此外,你不能像在这种情况下那样使用整数连接(加在一起)字符串,其中output是一个数字.
将其更改为
print "That's " + str(output) + " degrees Celsius!"
Run Code Online (Sandbox Code Playgroud)
要么
print "That's %d degrees Celsius!" % output
Run Code Online (Sandbox Code Playgroud)