Note that there are some explanatory texts on larger screens.

plurals
  1. PO"or" doesn't appear to function as it should in Python
    primarykey
    data
    text
    <p>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:</p> <pre><code>def answercheck(answer): solved = 0 while solved == 0: useranswer = raw_input("&gt; ") if useranswer == answer: solved = 1 correct() elif useranswer == "A" or "B" or "C" or "D": solved = 1 incorrect() else: print "False input. Try again." </code></pre> <p>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.</p> <p>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.</p> <p>So I rewrote the thing a little, and it appears to work when I do this:</p> <pre><code>def answercheck(answer): solved = 0 while solved == 0: useranswer = raw_input("&gt; ") 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." </code></pre> <p>This seems a little redundant to me. What is going wrong in the first script?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload