具有多个打印行的if语句中出现意外的缩进错误

时间:2014-01-29 18:00:46

标签: python indentation

当我在python启动器中运行我的python脚本时,收到此错误消息:

, line 45
print([l[0] for l in e])
^
IndentationError: unexpected indent

这是它所指的第二个代码:

flag = 0
num_comb = 1
comb = [c for i in range(len(menu)+1) for c in combinations(menu, i)]

for e in comb:
    if sum(l[1] for l in e) == maxSugar:
        print "The combination number " + str(num_comb) + " is:\n" 
        print([l[0] for l in e])
        print "\n\n\n"
        num_comb += 1 
        flag = 1

if flag == 0:
    print "there are no combinations of dishes for your sugar intake... Sorry! :D "

为什么它会给我一个错误的任何想法?如果我只包含一个打印语句print([l[0] for l in e]),程序运行正常!我是python的新手,所以不确定我是否被允许在print块中包含多个if语句。我不明白为什么不。

1 个答案:

答案 0 :(得分:1)

大多数情况下,此错误来自缩进中混合制表符和空格。 (根据PEP 8你根本不应该使用制表符进行缩进。)除此之外,我想知道你的代码是否可以在python3中运行,因为在某些情况下使用版本2样式的print语句。