0

Tying to account for a user entering an answer in lower or upper case. It doesn't work as the first statement is accepted as true regardless of what I enter. Perhaps I don't understand enough about booleans but with regards to that first statement if I don't enter "B" or "b" then it would be false resulting in the next statement being evaluated; or so I thought. Can someone explain what I'm getting wrong?

next = raw_input("Enter A, B, C or D: ")

if next == "B" or "b":
    print "Correct!"

elif next == "D" or "d":
    print "Fair enough, Ill give you this one."

elif next == "C" or "A":
    print "Incorrect"
    exit(0)

else:
    print "WRONG. START AGAIN"
    exit(0)
staredecisis
  • 157
  • 2
  • 3
  • 16

1 Answers1

1

You are lacking fundamental knowledge in how boolean logic works. I recommend a book or reading more online.

if next == "B" or "b":

should be

if (next == "B") or (next == "b"):

If you substitute letter variables for each variable in your code what you get is: A == B or C, and that evaluates to True always because in your case C is just a number above 0, which Python considers to be "truthy".

Eugene K
  • 3,381
  • 2
  • 23
  • 36
  • I would rather use `next.lower() == 'b'` or the upper() version , also I would rather not use `next` as a variable name, since it would shadow the builtin function `next`. – Anand S Kumar Sep 01 '15 at 04:13
  • Yea I'm doing that and this is part of it. Thanks for the knowledge! – staredecisis Sep 01 '15 at 04:15
  • 1
    I would specifically recommend finding a good Python tutorial online and reading the logic section. Each language handles boolean logic slightly differently (what counts as "true" and "false", what is returned, etc.) so it would help to see the "rules" laid out specifically for Python. – Dan Cusher Sep 01 '15 at 04:38