Loop - simple errors - stuck


#1



I am stuck at "Loops" - 4. Simple errors


This is my code:

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

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


I am stuck in the while loop even when I type y or n.
Also, I don't understand why I needed to use "and" and not "or"



#2

It must be indentation problem.

First you have to know that you enter in the loop only if the output of the condition in the while is true.

It is AND because AND returns true only if all the conditions are true. ie with AND you will enter in the loop only if choice is different from "y" and at the same time different from "n"...
choice can be anything.. but not "y" and "n".
eg: choice = "test"

But with OR you will always enter in the loop (infinite loop) because the operator OR always returns true if at least one condition is true and it will return false only if all the conditions are false.
In the case of this exercice, the 2 conditions can never be false at the same time because choice can't take the values "y" and "n" at the same time.... And according to the conditions, you can observe that only "y" and "n" can return false.


#3

Thank you for taking the time to explain! Once I thought about the having to need both of them to be false to not enter the loop i got it :slight_smile:


#4

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