Python中elif语句的语法错误

时间:2016-05-09 20:09:28

标签: python python-2.7 if-statement syntax-error

我在使用Python 2.7编写的代码时遇到了一些麻烦。它给我一个关于elif语句的语法错误,但是没有解释,我在代码中找不到任何合理的错误。 (字体是我定义的方法。)

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

1 个答案:

答案 0 :(得分:3)

events: [], animation: { duration: 0, onComplete:function () { var self = this; var elementsArray = []; Chart.helpers.each(self.data.datasets, function (dataset, datasetIndex) { Chart.helpers.each(dataset.metaData, function (element, index) { var tooltip = new Chart.Tooltip({ _chartInstance: self, _chart: self.chart, _data: self.data, _options: self.options, _active: [element] }, self); tooltip.update(); tooltip.transition(Chart.helpers.easingEffects.linear).draw(); }, self); }, self); } }

之前,您有一个未缩进的三引号字符串文字
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: # ... 三重引号字符串作为&#39;阻止评论&#39;,则必须缩进它们,使其成为""" ... """if块的一部分放在:

elif