By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,031 Members | 1,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,031 IT Pros & Developers. It's quick & easy.

Problem I have with a while loop/boolean/or

P: n/a
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...)

Mar 13 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
"is*******@googlemail.com" <is*******@googlemail.comwrites:
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.
Suppose your hint is 'y'. Ask yourself these two questions:

1. Is (hint != 'n') true?
2. Is (hint != 'y') true?

Now ask yourself what the condition in your while loop says.
Mar 13 '07 #2

P: n/a
is*******@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
Mar 13 '07 #3

P: n/a
On Mar 13, 10:43 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
"israph...@googlemail.com" <israph...@googlemail.comwrites:
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.

Suppose your hint is 'y'. Ask yourself these two questions:

1. Is (hint != 'n') true?
2. Is (hint != 'y') true?

Now ask yourself what the condition in your while loop says.
Thanks.

*puts my head under a white sheet* =/
Mar 13 '07 #4

P: n/a
On Mar 13, 10:50 pm, Larry Bates <lba...@websafe.comwrote:
israph...@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.

Mar 13 '07 #5

P: n/a
is*******@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()

>
Mar 13 '07 #6

P: n/a
John McMonagle wrote:
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()

I always try to stay away from 'negative' operators if possible, to
improve readability:

while True:
hint = raw_input('\nAre you stuck? y/n: ').lower()
if hint = 'y' or hint = 'n':
break
else:
print 'Please answer yes or no'
Mar 14 '07 #7

P: n/a
hg
is*******@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


Mar 14 '07 #8

P: n/a
hg
hg wrote:
is*******@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

oops, meant to answer to the main post

Mar 14 '07 #9

P: n/a
Bart Willems wrote:
...
I always try to stay away from 'negative' operators if possible, to
improve readability:

while True:
hint = raw_input('\nAre you stuck? y/n: ').lower()
if hint = 'y' or hint = 'n':
break
else:
print 'Please answer yes or no'
And if you really want to see what is going wrong, replace that last by:
print 'Please answer yes or no (not %r):' % hint

--
--Scott David Daniels
sc***********@acm.org
Mar 14 '07 #10

P: n/a
Scott David Daniels wrote:
Bart Willems wrote:
>...
I always try to stay away from 'negative' operators if possible, to
improve readability:

while True:
hint = raw_input('\nAre you stuck? y/n: ').lower()
if hint = 'y' or hint = 'n':
break
else:
print 'Please answer yes or no'

And if you really want to see what is going wrong, replace that last by:
print 'Please answer yes or no (not %r):' % hint
Better still, use

Please answer y or n

since those are the values you are testing for ...

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007

Mar 14 '07 #11

P: n/a
On Mar 13, 5:34 pm, "israph...@googlemail.com"
<israph...@googlemail.comwrote:
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

Mar 14 '07 #12

P: n/a
On Mar 13, 7:34 pm, "israph...@googlemail.com"
<israph...@googlemail.comwrote:
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.
Why don't you try this instead:

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

while not hint in 'yn':
hint = raw_input("Please specify a valid choice: ")

[]s
FZero

Mar 14 '07 #13

P: n/a
En Wed, 14 Mar 2007 15:34:20 -0300, Fabio FZero <fa*********@gmail.com>
escribió:
Why don't you try this instead:

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

while not hint in 'yn':
hint = raw_input("Please specify a valid choice: ")
Dangerous: what if the user types 'yn'?
`hint in list('yn')` may be better, if you don't like the previous answers.

--
Gabriel Genellina

Mar 16 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.