Problem I have with a while loop/boolean/or

israphelr at googlemail.com israphelr at googlemail.com
Tue Mar 13 18:54:24 EDT 2007


On Mar 13, 10:50 pm, Larry Bates <lba... at websafe.com> wrote:
> israph... 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

Thanks Larry.




More information about the Python-list mailing list