Simple Errors


Hello. I realize the accepted code for this lesson utilizes 'and.' I am curious why a more minimal formulation using 'or' can't be used instead. Below is my code which only accepts 'y.' Is there a way to use 'or' on the right of '!=" instead of using 'and' and typing out 'choice !=...' again for the second option?

choice = raw_input('Enjoying the course? (y/n)')

while choice != ('y' or 'n'):  # Fill in the condition (before the colon)
    choice = raw_input("Sorry, I didn't catch that. Enter again: ")


We need to evaluate each expression independently:

while choice != 'y' and choice != 'n':


So what is happening when my code is run? It simply checks for the first expression (i.e. 'y') and ignores the rest of the condition before running the loop?


@jedhafner ,

See the documentation here: 5.2. Boolean Operations — and, or, not.

You have been comparing choice to the expression 'y' or 'n'.

The or operator takes two operands. If the first operand does not have a value that corresponds to False, then the or operator returns the value of the first operand without evaluating the second operand. Therefore 'y' or 'n' evaluates to 'y'. So, essentially, you have been comparing choice to 'y', which is not what needs to be done in this exercise.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.