2
HelloThere = ["Hello There", "Hello there", "hello there"]
for word in HelloThere:
    if (message.content.lower().count(word) > 0) and (message.author.id != 712885407993561169):
        await message.channel.send("General Kenobi")

I am trying to test to see if a string contains a certain key phrase. If the string contains something like, "Why Hello THeRe" then it must satisfy the if statement.

The code returns General Kenobi correctly, however, I want to have good code practice and do not understand why the list of words (HelloThere = ["Hello There", "Hello there", "hello there"]) only works when it's in this certain order and number. If for example I just have it as Hello There = ["Hello There"] and test for that, it won't work.

3
  • I don't understand your question. In the former case HelloThere is a list while in the latter it's a str. The for ... in loop will obviously work differently in each case. Commented Jan 12, 2021 at 2:06
  • how so, because I want to understand how this works Commented Jan 12, 2021 at 2:07
  • Unfortunately Stack Overflow format is not suitable to explain this, but any introductory Python tutorial would help. Commented Jan 12, 2021 at 2:08

1 Answer 1

1

You're testing the lowercased version of the message (.lower()) so the variants with uppercase letters can never match. The point of using .lower() in the first place is to avoid having to do any of this -- just test the lowercase phrase against the lowercased message so you can ignore capitalization entirely.

if "hello there" in message.content.lower() and message.author.id != 712885407993561169:
    await message.channel.send("General Kenobi")

If you did have multiple phrases to check, it'd be more simple to use any:

phrases = {"hello there", "you are a bold one"}
if message.author.id != 712885407993561169 and any(
    p in message.content.lower() for p in phrases
):
    await message.channel.send("General Kenobi")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.