Problem I have with a while loop/boolean/or

Bart Willems b.r.willems at gmail.com
Tue Mar 13 21:17:48 EDT 2007


John McMonagle wrote:
> Try it a different way:
> 
> while True:
>     hint = raw_input("\nAre you stuck? y/n: ")
>     hint = hint.lower()
>     if hint != 'y' and hint != 'n':
>         print "Please answer y or n"
>         continue
>     else:
>         break
> if hint == 'y':
>     do_your_hint_stuff()


I always try to stay away from 'negative' operators if possible, to 
improve readability:

while True:
    hint = raw_input('\nAre you stuck? y/n: ').lower()
    if hint = 'y' or hint = 'n':
       break
    else:
       print 'Please answer yes or no'



More information about the Python-list mailing list