Reputation: 8798
I'm trying to put multiple conditions in my if statement in Python like below:
if (h9 == h1 or h9 == h2 or h9 == h3 or h9 == h4 or h9 == h5 or h9 == h6 or h9 == h7 or h9 == h8) and (h10 == h1 or h10 == h2 or h10 == h3 or h10 == h4 or h10 == h5 or h10 == h6 or h10 == h7 or h10 == h8) :
do sth.
Basically it's OR condition for both h9
and h10
at the same time.
However, this doesn't work, and gives errors like:
IndentationError: unindent does not match any outer indentation level
What's the problem?
Upvotes: 0
Views: 7475
Reputation: 16440
If you must use long conditionals, you can enclose your entire if condition in parenthesis, you can put it on multiple lines without indentation errors.
a = 2
b = 2
if ((a == 1 or a == 2) and
(b == 1 or b == 2)):
print "hello"
Upvotes: 3
Reputation: 46183
No idea about the indentation error without more context, but here's something you can do to make the if
condition shorter:
lst = [h1, h2, h3, h4, h5, h6, h7, h8]
if h9 in lst and h10 in lst:
pass
Also, you should really use more descriptive variable names.
Oh, and if you have a lot of elements and plan to do a lot of searching, you might favor sets:
s = set([h1, h2, h3, h4, h5, h6, h7, h8]) # {h1, h2, h3, ...} in Python 3.x
if h9 in s and h10 in s:
pass
Upvotes: 7
Reputation: 33387
Another option is:
if any(h in {h1,h2,...} for h in (h9, h10)):
Upvotes: 0
Reputation: 64563
There is no indentation problem here.
I suppose that there is an indentation problem inside do sth.
.
Upvotes: 0