0

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
  • You've misunderstood the `or` statement. Your condition can be rewritten `if ('SOIL') or ('soil' in a.value):`, the first part of the condition will always be `True` since it is a non-empty string – Iain Shelvington Jul 20 '20 at 04:10
  • No, it should be `if 'SOIL' in a.value or 'soil' in a.value` if the desired effect is to check if either 'SOIL' or 'soil' occur in a.value. – Alex Couch Jul 20 '20 at 04:11
  • You have to check both the words separately with a or in between `("soil" in a.value) or ("SOIL" in a.value)` – Ezio Jul 20 '20 at 04:12

1 Answers1

1

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:
user2864740
  • 60,010
  • 15
  • 145
  • 220