If the goal is just to say if any of the known list appears in userMessage
, and you don't care which one it is, use any
with a generator expression:
if any(srchstr in userMessage for srchstr in ('hi', 'hello', 'greetings')):
It will short-circuit when it gets a hit, so if hi
appears in the input, it doesn't check the rest, and immediately returns True
.
If the words must be found as individual words (so userMessage = "This"
should be false, even though hi
appears in it), then use:
if not {'hi', 'hello', 'greetings'}.isdisjoint(userMessage.split()):
which also short-circuits, but in a different way; it iterates userMessage.split()
until it matches one of the keywords, then stops and returns False
(which the not
flips to True
), returning True
(flipped to False
by not
) only if none of the words matches a keyword.