Loops/exercises/simple-errors

choice = raw_input(‘Enjoying the course? (y/n)’)
while choice != ‘y’ and choice != ‘n’:
choice = raw_input("Sorry, I didn’t catch that. Enter again: ")

In this code, I wish that if the user has entered anything except ‘y’ or ‘n’ a re-prompt appears. But I am not able to comprehend: why we need to used ‘and’ and not ‘or’. For, the entered input can be either ‘y’ or ‘n’. So any one of the condition must be satisfied but ans requires both are to be satisfied. Please help!

the simplest way to figure this out is to walk through the flow, lets say the user enter something invalid (x for example), we get:

while 'x' != 'y' and 'x' != 'n'

which result in:

while true and true:

which is true. so the loop will make another iteration.

now lets say the user enter y (a valid choice):

while 'y' != 'y' and 'y' != 'n'

we get:

while false and true

which is false. which is desired, we got valid input, so we don’t need to re-prompt

don’t get confused by the not equal (!=) operator.

1 Like

Thank you for help :slight_smile: