
我真的无法理解我做错了什么。非常感谢您的帮助。
你的缩进是错误的。左缩进一次,语法错误就会消失。
if < Condition Here >:
# Do Something
elif < Condition Here >:
将给出语法错误,而
if < Condition Here>:
# Do Something
elif < Condition Here >:
# Do Something
是正确的缩进。缩进在 Python 中很重要。
来自Python 文档,
逻辑行开头的前导空格(空格和制表符)用于计算行的缩进级别,而缩进级别又用于确定语句的分组。
您的elif陈述没有与if陈述正确缩进:
if some_condition:
#code
elif some_other_condition:
#code
从文档:
逻辑行开头的前导空格(空格和制表符)用于计算行的缩进级别,而缩进级别又用于确定语句的分组。
来自文档的示例:
>>> x = int(raw_input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
... x = 0
... print 'Negative changed to zero'
... elif x == 0:
... print 'Zero'
... elif x == 1:
... print 'Single'
... else:
... print 'More'
...
More
在 IDLE 上尝试这样的事情:
>>> x = 2
>>> if x == 0:
print x
elif x == 1:
print x
elif x == 2:
print x
else:
print 'foo'
2
例如,您应该创建一个单独的 python 脚本tmp.py并将最复杂的命令序列放在那里:
if something:
pass
else:
pass
保存它,然后在run tmp您使用IPython或python tmp.py使用 shell(命令提示符、bash 等)时运行。