vendredi 2 juillet 2021

Finding a word in given text using nested and/or along with in/not in operators

Hi i have a string and i am trying to print some message when a certain condition found. Code i am using -

text = """The nearest paid public fire department 24/7 is located 1km"""

if ((("public" in text.lower()) or ("Public" in text.lower()))
    and (("fire department" in text.lower()))
    and (("full time" not in text.lower()) or ("full-time" not in text.lower()) or ("24/7" not in text.lower()))):
     print("Keyword not present in text")
else:
     print("Keyword present")

Output - Keyword not present in text

The output is wrong because 24/7 is present in the text and ideally it should print "Keyword present".

I can't split the sentence and then try looking for words because certain words i want to find as it is mentioned. For example "fire department".

Can you please suggest where i am going wrong.

Aucun commentaire:

Enregistrer un commentaire