Mostly this error comes from mixing tabs and spaces in your indentation. (According to PEP 8 you shouldn't use tabs for indentation at all.) Apart from that I wonder if your code can run in python3 as you use version 2 style print statements in some cases.
Unexpected indent error in an if statement with multiple print lines
-
04-10-2022 - |
Question
When I run my python script in python launcher, I get this error message:
, line 45
print([l[0] for l in e])
^
IndentationError: unexpected indent
This is the second of code that it is referring to:
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 "
Any ideas as to why it is giving me an error? If I only include one print statement print([l[0] for l in e])
the program runs fine! I am pretty new to python so not sure if I am allowed to include multiple print
statements in an if
block. I wouldn't see why not though.
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow