0

I have the following

items=' g energy 4"3/4 drilling jar'
print(items.split())
if 'energy' or 'ge' in items.split():
    print('E')
if 'slb' or 'schlumberger' in items.split():
    print('S')
if 'oes' or 'overseas' in items.split():
    print('O')

output 
E
S
O

what I want is to check if any of the words are in the string but what i am getting is that it tells me all the words are in the string i want to check the word not the characters of the word how can I go around to achieve that?

yosef ali
  • 33
  • 8
  • 1
    Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Ture Pålsson Oct 04 '19 at 07:55
  • I am not sure I tried to prevent that by using split however when i remove the or from the if statement it works perfectly then but with or it just matches every string and says any word is in that string even if its not – yosef ali Oct 04 '19 at 07:57

1 Answers1

1

your understanding of or is not quite correct.

If we take one of you if line: if 'slb' or 'schlumberger' in items.split() and use print to evaluate its truthness

#you are essentially saying this
print(bool('slb'))
#or this
print(bool('schlumberger' in items.split()))

OUTPUT

True
False

So you can see 'slb' will return true and 'schlumberger' in items.split() returns false.

As you are using an or then anything to the left of the or will be evaluated first. Since 'slb' returns true the or will not check the other side and will just say that this if is true and print your letter.

instead you need to check each string in items seperated with an or.

items=' g energy 4"3/4 drilling jar'.split()
print(items)
if 'energy' in  items or 'ge' in items:
    print('E')
if 'slb' in items or 'schlumberger' in items:
    print('S')
if 'oes' in items or 'overseas' in items:
    print('O')
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42