Problem I have with a while loop/boolean/or

Larry Bates lbates at websafe.com
Tue Mar 13 18:50:37 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...)
> 

Your if test is wrong.

If hint='n'

hint !='n' is False
hint !='y' is True

False or True equals True

I think you want something like:

hint = raw_input("\nAre you stuck? y/n: ")
hint = hint.lower()

while hint not in ['y', 'n']:
    hint = raw_input("Please specify a valid choice: ")

-Larry



More information about the Python-list mailing list