python中elif语句的语法错误

Syntax Error on elif statement in Python

我在用Python2.7编写的代码中遇到了一些问题。它给了我一个关于elif语句的语法错误,但是没有解释,我在代码中找不到任何合理的错误。(typeline是我定义的方法。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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."]

错误:

1
2
3
4
  File"<stdin>", line 9
    elif num >= -10 and num < 0:
       ^
SyntaxError: invalid syntax


elif之前有一个未插入的三重引号字符串文字:

1
2
""" Mildly poisonous"""
elif num >= -10 and num < 0:

"""..."""字符串不是多行注释。它们创建字符串,只有当您忽略生成的字符串对象时,python才会忽略该行。它们仍然是Python语法的一部分;使用缩进规则时不能忽略它们。

使用适当的#注释代替:

1
2
3
4
5
6
7
8
9
# Toxic
if num >= -25 and num < -10:
    # ...
# Mildly poisonous
elif num >= -10 and num < 0:
    # ...
# Healthy
else:
    # ...

由于注释被语法完全忽略,所以不管它们是如何缩进的。

如果必须使用""" ..."""三重引号字符串作为"块注释",则必须将它们缩进为ifelif块的一部分,它们放置在:

1
2
3
4
5
6
7
8
9
""" Toxic"""
if num >= -25 and num < -10:
    # ...
   """ Mildly poisonous"""
elif num >= -10 and num < 0:
    # ...
   """ Healthy"""
else:
    # ...