Python while loop condition check for string -


in codeacademy, ran simple python program:

choice = raw_input('enjoying course? (y/n)')  while choice != 'y' or choice != 'y' or choice != 'n' or choice != 'n':  # fill in condition (before colon)     choice = raw_input("sorry, didn't catch that. enter again: ") 

i entered y @ console loop never exited

so did in different way

choice = raw_input('enjoying course? (y/n)')  while true:  # fill in condition (before colon)     if choice == 'y' or choice == 'y' or choice == 'n' or choice == 'n':         break     choice = raw_input("sorry, didn't catch that. enter again: ") 

and seems work. no clue why

you have logic inverted. use and instead:

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

by using or, typing in y means choice != 'y' true, other or options no longer matter. or means one of options must true, , given value of choice, there @ least 1 of != tests going true.

you save typing work using choice.lower() , test against y , n, , use membership testing:

while choice.lower() not in {'n', 'y'}: 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -