Why is my if statement true when it should be false

Question:

My if statement is returning the wrong values:

for i in range (3325 , 3325+1):
    FSBRID = []
    for j in range(93, 94):
        a = ws1.cell(row=i, column=j)
        print(a.value)
        if 'SOIL' or 'soil' in a.value:
            print('wrong')

The returned values:

TISSUE 
wrong

Process finished with exit code 0
Asked By: Elinor Martin

||

Answers:

The construct

if 'SOIL' or 'soil' in a.value:

is parsed as though it were

if ('SOIL') or ('soil' in a.value):

and non-empty strings are true, making it equivalent to

if True or ('soil' in a.value):

One solution is to split it into two “in” operators, the results of which is combined.

if 'SOIL' in a.value or 'soil' in a.value:
Answered By: user2864740
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.