[英]How can I check if multiple words appear in a sentence?
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"我的意思是,如果句子中出现任何单词“是”或“是”(甚至更多),那么机器人必须发送“是”,如果不是,那么它应该说“不”
You can check against multiple values using a combination of in
and any
.您可以使用in
和any
的组合来检查多个值。 It essentially creates a list of boolean values, then checks if any of them are true (ie 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该解决方案基于此答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.