Home > database >  How can I check if multiple words appear in a sentence?
How can I check if multiple words appear in a sentence?

Time:04-06

I seem unable to find the solution as to why this doesn't work here's the line of code:

if ("yeah" or "yes") in message.content:
  await message.channel.send("yep")
else:
  await message.channel.send("nope")

I tried using any and even a word list but it didn't work either

what I mean to say is that if any of the words "yeah" or "yes" (or even more) appear in a sentence then the bot must send "yep" if not then it should say "nope"

CodePudding user response:

You can check against multiple values using a combination of in and any. It essentially creates a list of boolean values, then checks if any of them are true (i.e. one of the words exists in the message).

if any(x in message.content for x in ["yeah", "yes"]):
    # send "yep"
else:
    # send "nope"

This solution is based off this answer

  • Related