0

I have this little python code, it doesn't work as below.

while True:
    activity = input("What would you like to do today? ")
    a="cinema"
    b="tennis"
    if a or b not in activity.lower():
        print(f"Instead of {activity} I want to go to the cinema")
    else:
        print("Sure Let's do that!!")

However when I extract the or b , it works fine


while True:
    activity = input("What would you like to do today? ")
    a="cinema"
    b="tennis"
    if a  not in activity.lower():
        print(f"Instead of {activity} I want to go to the cinema")
    else:
        print("Sure Let's do that!!")

The question is why doesn't a or b doesn't when used with not in work?

EDIT: I have done the below modification, But still doesn't work.

while True:
    activity = input("What would you like to do today? ")
    a="cinema"
    b="tennis"
    if (a not in activity.lower()) or (b not in activity.lower()):
        print(f"Instead of {activity} I want to go to the cinema")
    else:
        print("Sure Let's do that!!")

Below link is also suggested for this post. It is also very good. So I share it for the ones who will benefit from

How to test multiple variables against a value?

4 Answers4

1

Look at the value of a or b:

>>> a = "cinema"
>>> b = "tennis"
>>> a or b
'cinema'

The "or" operator doesn't have a special different meaning when it's part of an in.

Samwise
  • 68,105
  • 3
  • 30
  • 44
  • While it's true that `or` works this way, the operator precedence is not what you expect. For example, if you add `c = {'cinema'}`, then `a or b not in c` results in `'cinema'` (the same as `a or (b not in c)`, whereas `(a or b) not in c` results in `False`. – Karl Knechtel Nov 14 '20 at 22:08
1

This won't work, it's not doing what you think:

if a or b not in activity.lower():

The above is equivalent to:

if (a) or (b not in activity.lower()):

Which means something different, and will be True as long as a is not null or empty. Besides you have the comparison backwards, the correct syntax you're looking for is:

if activity.strip().lower() not in (a, b):
Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

a or b not in c is treated as (a) or (b not in c). a is considered True if it is non-zero or non-empty. So if a is a non-empty string, a or b not in c will always be True.

c not in (a,b) is what you want. It tests that the value of c is not a member of the container, equivalent to c != a and c != b.

In your edit, activity is a string, and in in strings works more like contains. So this code:

activity = "run"
a="cinema"
b="tennis"
if (a not in activity.lower()) or (b not in activity.lower()):

is equivalent to:

'cinema' not in 'run' or 'tennis' not in 'run'

which is:

True or True  # result True

Note 'tennis' in 'thetennisball' is True because 'thetennisball' contains the word 'tennis'. If you are looking for exact matches this isn't what you want.

Another issue is using or. When testing that a thing is not one thing or another, one of them will always be false, since a thing can't be two things at once. You'll never get a True for x != 2 or x != 1 for example. If x is 1 it is not 2 and if x is 2 it is not 1. Use and instead.

To test for words in an exact list of words, use a list (or other container like tuple):

'tennis' not in ('cinema','tennis') # False
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • The only part I don't understand is about my edit. You say tennis="run" but in the "tennis" !="r" part you claim it is going to search for each letter. That surely returns false. Doesn't it search as a whole container? – Çağlar Yıldız Nov 14 '20 at 22:21
  • @ÇağlarYıldız Actually, I misspoke. *string* in *string* works differently. `'def' in 'abcdefghi'` returns True. `in` for strings is more like *contains*. I'll edit the answer to correct that, but still not what you want. – Mark Tolonen Nov 14 '20 at 23:33
-1

First, it is checking if (a) or (b not in activity.lower()) This is not right. What you want is something to check for a string in a string like if activty.count(a) == 0 and activty.count(b) == 0 becuase this checks how many times a and b appear in activity.

Shark Coding
  • 143
  • 1
  • 10