Python 中 elif 语句的语法错误
Syntax Error on elif statement in Python
我在 Python 2.7 中编写的这段代码遇到了一些麻烦。它在 elif 语句上给我一个语法错误,但没有任何解释,我在代码中找不到任何合理的错误。 (typeline是我定义的方法。)
num = randrange(-25,15)
""" Toxic """
if num >= -25 and num < -10:
responses = ["Ugh, nasty.", "That was absolutely disgusting.", "My stomach feels like it's going to explode.", "Pardon me if I puke."]
typeline(responses[randrange(0,4)], "jack")
return [num, "Jack ate a VERY TOXIC FRUIT and survived.", "Jack ate a VERY TOXIC FRUIT and died."]
""" Mildly poisonous """
elif num >= -10 and num < 0: """ SYNTAX ERROR HERE """
responses = ["Yuck", "It's kinda bitter.", "Tastes like an unripe banana.", "It's not so bad."]
typeline(responses[randrange(0,4)], "jack")
return [num, "Jack ate a MILDLY TOXIC FRUIT and survived.", "Jack ate a MILDLY TOXIC FRUIT and died."]
""" Healthy """
else:
responses = ["Definitely not too bad", "It's almost kind of tasty!", "Should I make a jam out of this?", "This is my new favorite fruit."]
typeline(responses[randrange(0,4)], "jack")
return [num, "Jack ate a HEALTHY FRUIT and was rescued.", "Jack ate HEALTHY FRUIT and survived."]
错误:
File "<stdin>", line 9
elif num >= -10 and num < 0:
^
SyntaxError: invalid syntax
您在 elif
:
之前有一个未缩进的三引号字符串文字
""" Mildly poisonous """
elif num >= -10 and num < 0:
"""..."""
字符串文字是 而不是 多行注释。他们创建字符串,只是因为您随后忽略了生成的字符串对象,所以 Python 才会忽略该行。它们仍然是 Python 语法的一部分;使用时不能忽略缩进规则。
改为使用正确的 #
评论:
# Toxic
if num >= -25 and num < -10:
# ...
# Mildly poisonous
elif num >= -10 and num < 0:
# ...
# Healthy
else:
# ...
由于注释完全被语法忽略,所以它们如何缩进并不重要。
如果您必须使用 """ ... """
三引号字符串作为 'block comments',您必须将它们缩进以成为它们所在的 if
或 elif
块的一部分:
""" Toxic """
if num >= -25 and num < -10:
# ...
""" Mildly poisonous """
elif num >= -10 and num < 0:
# ...
""" Healthy """
else:
# ...
我在 Python 2.7 中编写的这段代码遇到了一些麻烦。它在 elif 语句上给我一个语法错误,但没有任何解释,我在代码中找不到任何合理的错误。 (typeline是我定义的方法。)
num = randrange(-25,15)
""" Toxic """
if num >= -25 and num < -10:
responses = ["Ugh, nasty.", "That was absolutely disgusting.", "My stomach feels like it's going to explode.", "Pardon me if I puke."]
typeline(responses[randrange(0,4)], "jack")
return [num, "Jack ate a VERY TOXIC FRUIT and survived.", "Jack ate a VERY TOXIC FRUIT and died."]
""" Mildly poisonous """
elif num >= -10 and num < 0: """ SYNTAX ERROR HERE """
responses = ["Yuck", "It's kinda bitter.", "Tastes like an unripe banana.", "It's not so bad."]
typeline(responses[randrange(0,4)], "jack")
return [num, "Jack ate a MILDLY TOXIC FRUIT and survived.", "Jack ate a MILDLY TOXIC FRUIT and died."]
""" Healthy """
else:
responses = ["Definitely not too bad", "It's almost kind of tasty!", "Should I make a jam out of this?", "This is my new favorite fruit."]
typeline(responses[randrange(0,4)], "jack")
return [num, "Jack ate a HEALTHY FRUIT and was rescued.", "Jack ate HEALTHY FRUIT and survived."]
错误:
File "<stdin>", line 9
elif num >= -10 and num < 0:
^
SyntaxError: invalid syntax
您在 elif
:
""" Mildly poisonous """
elif num >= -10 and num < 0:
"""..."""
字符串文字是 而不是 多行注释。他们创建字符串,只是因为您随后忽略了生成的字符串对象,所以 Python 才会忽略该行。它们仍然是 Python 语法的一部分;使用时不能忽略缩进规则。
改为使用正确的 #
评论:
# Toxic
if num >= -25 and num < -10:
# ...
# Mildly poisonous
elif num >= -10 and num < 0:
# ...
# Healthy
else:
# ...
由于注释完全被语法忽略,所以它们如何缩进并不重要。
如果您必须使用 """ ... """
三引号字符串作为 'block comments',您必须将它们缩进以成为它们所在的 if
或 elif
块的一部分:
""" Toxic """
if num >= -25 and num < -10:
# ...
""" Mildly poisonous """
elif num >= -10 and num < 0:
# ...
""" Healthy """
else:
# ...