0

I am coding a reddit bot in python that answers specific questions.

Here is the code:

    if comment.id not in comments_replied_to:
        if comment.author != r.user.me():

            if "First Question" or "first q" in comment.body:
                comment.reply("First Answer")
                comments_replied_to.append(comment.id)
            elif "Second Question" in comment.body:
                comment.reply("Second Answer")
                comments_replied_to.append(comment.id)

When I type a comment with:

First Question

the response is:

First Answer

When I type in:

Second Question

the response is:

First Answer

Why is it only firing the first statement?

JamesG
  • 1,552
  • 8
  • 39
  • 86
  • 2
    Do `if "First Question" in comment.body or "first q" in comment.body:` ... – L. MacKenzie Oct 13 '17 at 13:10
  • `"First Question" or "first q" in comment.body` doesn't do what you think it does. Please see the linked question for details. – PM 2Ring Oct 13 '17 at 13:13
  • @L.MacKenzie YES!! Thanks. You were first and quick - please leave a proper reply below and I will accept when I am able to :) – JamesG Oct 13 '17 at 13:13
  • Also see [Why does `a == b or c or d` always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true) and [Why does checking a variable against multiple values with `OR` only check the first value?](https://stackoverflow.com/questions/18212574/why-does-checking-a-variable-against-multiple-values-with-or-only-check-the-fi) – PM 2Ring Oct 13 '17 at 13:15

1 Answers1

1

if "First Question" or "first q" in comment.body: is always true because the first part (before the or) reads if "First Question" and a non-empty string has the boolean value of true.

So, change it to:

if "First Question" in comment.body or "first q" in comment.body:
elzell
  • 2,228
  • 1
  • 16
  • 26