0

I have a String message

message = "The boy Akash is studying in 9th class at Boys school"

I wanted to check if any of the words boy or class or school is not present. If any of those are not present it should show a error message.

if('boy' or 'class' or 'school' not in message):
    print = "Please check if you have filled all data"

But when I tried this, even if all keywords are present its showing error. Where it could be wrong.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
wanderors
  • 2,030
  • 5
  • 21
  • 35

3 Answers3

3

It doesn't work because what you wrote is not what you meant.

Consider the snippet:

if 'boy':
    print("There's a boy here")

If you run it, you'll get:

>>> There's a boy here!

This is because by default, python consider all non-empty string as True.

Therefore, to fix your code, you need to:

if('boy' not in message or 'class' not in message or 'school' not in message):
    print = "Please check if you have filled all data"

or, equivalently:

for word in ['boy', 'class', 'school']:
    if word not in message:
        print = "Please check if you have filled all data"
Neb
  • 2,270
  • 1
  • 12
  • 22
  • Upvoted this as the "equivalent" solution (`for word in ...`) seems more Pythonic than using a regex - "Readability counts". – PeterByte Jul 19 '21 at 11:30
2

Presumably you have to list out each expression separately:

if "boy" not in message or "class" not in message or "school" not in message:
    print = "Please check if you have filled all data"

You could also use regex here, which would allow you to use word boundaries, for a possibly more reliable match:

match1 = re.search(r'\bboy\b', message)
match2 = re.search(r'\bclass\b', message)
match3 = re.search(r'\bschool\b', message)
if not match1 or not match2 or not match3:
    print = "Please check if you have filled all data"
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • May be it easier to use the set? `if len(set(re.findall(r'boy|class|school', message))) != 3: print('Please check if you have filled all data')`. – Andrei Odegov May 19 '19 at 10:08
  • 1
    @AndreiOdegov Clever idea, but what happens if the message should only contain the keyword `boy`, but three times? Then there would be a false negative. – Tim Biegeleisen May 19 '19 at 10:13
  • Multiset? `from collections import Counter` `message = 'The boy Akash is the boy and studying in 9th class at Boys school'` `multiset = Counter()` `multiset.update(re.findall(r'\b(?:boy|class|school)\b', message))` `print(multiset)`. Output: `Counter({'boy': 2, 'class': 1, 'school': 1})`. – Andrei Odegov May 19 '19 at 10:31
1

What is your error?

Your syntax should be:

if ('boy' not in message) or ('class' not in message) or ('school' not in message) :
    print("Please check if you have filled all data") 

If you have a conditional with multiple expressions, you must give each expression separated by a logical operator (or, and) , as above but also for more complex decision structures eg:

if ( (x < 2) and (y > 5) ) or (z == 0):
    print("") 

Note the print statement - you have tried to assign a string to a variable print as opposed to using the print function with a string as argument. As the print keyword is reserved as a standard library function, you shouldn't be able to use it as a variable.

Dagorodir
  • 104
  • 1
  • 10