Problem I have with a while loop/boolean/or

John McMonagle jmcmonagle at velseis.com.au
Tue Mar 13 19:08:05 EDT 2007


israphelr at googlemail.com wrote:
> Hi all.
> 
> I have a problem with some code :(
> 
> -----------
> 
> hint = raw_input("\nAre you stuck? y/n: ")
> hint = hint.lower()
> 
> while (hint != 'n') or (hint != 'y'):
>     hint = raw_input("Please specify a valid choice: ")
> 
> ---------------------------------------------
> 
> so everytime I run the program, and enter my choice as y or n, I'm
> told to 'Please Specify a valid Choice Again' and can't get out of the
> loop.
> 
> 
> 
> -----------
> 
> hint = raw_input("\nAre you stuck? y/n: ")
> hint = hint.lower()
> 
> while (hint != 'n'):
>     hint = raw_input("Please specify a valid choice: ")
> 
> ---------------------------------------------
> 
> As for here when I enter n, I can leave the while loop.
> 
> Anyway I can't put my finger on this, so I'd be real grateful if
> someone could tell me what I've done wrong. Thanks.
> (I have a guy feeling it's something really silly I'm overlooking...)

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()


> 




More information about the Python-list mailing list