1

我在最后一行 - 第 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."
4

1 回答 1

9

您忘记了最后一个冒号 ( :) else

还:

input ("Convert to Celsius or Fahrenheit?")

应该改为

raw_input ("Convert to Celsius or Fahrenheit?")

asinput()尝试评估其输入,同时raw_input采用“原始”字符串。c例如,当您输入时,input()它会尝试评估表达式c,就好像它是查找变量的 Python 代码一样,craw_input只是获取字符串而不尝试评估它。

此外,您不能像在这种情况下那样将字符串与整数连接(相加),其中output是一个数字。

将其更改为

print "That's " + str(output) + " degrees Celsius!"

或者

print "That's %d degrees Celsius!" % output
于 2012-04-12T15:27:57.800 回答