Problem I have with a while loop/boolean/or

hg hg at nospam.org
Tue Mar 13 21:17:54 CET 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...)

Hi,

Whatever hint is:
(hint != 'n') or (hint != 'y')
is always going to be true

as it cannot really be both at the same time 

use "and" instead of "or"

hg









More information about the Python-list mailing list