Problem I have with a while loop/boolean/or

gslindstrom at gmail.com gslindstrom at gmail.com
Wed Mar 14 15:56:55 CET 2007


On Mar 13, 5:34 pm, "israph... at googlemail.com"
<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: ")
>

I'd even make it a little more bullet-proof by using lower()

while hint.lower() not in ('y','n'):
  stuff

That way if the user types in 'Y' on 'N' it will still work.  I've
even gone further on occasion

while hint.lower()[0] not in ('y','n'):
  stuff

so if they type 'Yes' or 'No' you're still good.

--greg




More information about the Python-list mailing list