Simple errors


#1




even when I enter y and n the code still continues


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

while choice != 'y' or choice != 'n':
    choice = raw_input("Sorry, I didn't catch that. Enter again: ")


#2

when you enter y, agree that the conditions are evaluated the following way:

while False or True:

'y' != 'y' would be false, and 'y' != 'n' would be true

so True or False would result in True, so the loop keeps running

if you enter something invalid like 'x' you get:

while  `'x' != 'y'` or `'x' != 'n'`

which is True or True , this results in True

So when the enters something valid, we get one False. How do we make sure False and True get evaluated to False? Which other operators do we have besides or?


4. Simple errors
#3

This is my solution and it worked

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'm not too familiar with posting the code with the indentation so it came out looking like that


#4

and why do you post your solution?


#5

Was there something wrong with the answer?
It worked for me


#6

no, there is nothing wrong with it, i just ask why you post it in this topic (i am curious for the reason)


#7

I was stuck on the same thing and I really appreciate your explanation, Stetim.


#8

2 posts were split to a new topic: Why I can't use y 'and' n?


#9

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