I'm trying to learn Python, and I've stumbled across this incredibly strange anomality whilst programming a "quiz" for my own learning purposes. The boolean "or" function is acting a little weird. Here's a piece of the code:
def answercheck(answer):
solved = 0
while solved == 0:
useranswer = raw_input("> ")
if useranswer == answer:
solved = 1
correct()
elif useranswer == "A" or "B" or "C" or "D":
solved = 1
incorrect()
else:
print "False input. Try again."
Here's what happens. The answercheck function is called with the real answer as argument after a quiz question is printed out for the quiz contestant. The user is then prompted for an answer. Because I want the only valid answers to be "A", "B", "C" or "D", I've put the thing in a "while" loop.
So, I would suspect, if someone made 'useranswer' something useless like 'lol' through raw_input, it would prompt the user again until "A", "B", "C" or "D" is inserted as answer. It doesn't, however, and takes any answer that isn't 'answer' as incorrect.
So I rewrote the thing a little, and it appears to work when I do this:
def answercheck(answer):
solved = 0
while solved == 0:
useranswer = raw_input("> ")
if useranswer == answer:
solved = 1
correct()
elif useranswer == "A":
solved = 1
incorrect()
elif useranswer == "B":
solved = 1
incorrect()
elif useranswer == "C":
solved = 1
incorrect()
elif useranswer == "D":
solved = 1
incorrect()
else:
print "False input. Try again."
This seems a little redundant to me. What is going wrong in the first script?