这个python代码中的语法错误在哪里?

时间:2012-04-12 15:27:04

标签: python syntax

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

1 个答案:

答案 0 :(得分:9)

您在最后:之后忘记了冒号(else)。

此外:

input ("Convert to Celsius or Fahrenheit?")

应改为

raw_input ("Convert to Celsius or Fahrenheit?")

input()尝试评估其输入时raw_input采用'原始'字符串。当您在c中输入input()时,它会尝试评估表达式c,就好像它是查找变量c的python代码而raw_input只需获取字符串而不尝试评估它。

此外,在output为数字的情况下,您不能像使用整数那样将字符串连接(加在一起)。

将其更改为

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

print "That's %d degrees Celsius!" % output
相关问题