473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Problem I have with a while loop/boolean/or

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
13 1681
"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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jagdip Singh Ajimal | last post by:
I have a new VB 6 project, and I have successfully created a data environment that connects to my oracle server (they don't make this easy!!). In my oracle server, I have the following stored...
2
by: Rob | last post by:
I have a public class setup as follows Public Class Variables Shared Property FranFormOpen() As Boolean Get Return FranFormOpen End Get Set(ByVal Value As Boolean) FranFormOpen = Value
1
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
3
by: Eugene | last post by:
I'm trying to write a class which uses BinaryWriter as its base but allows for queuing of write requests Public Class QueuedBinaryWriter Inherits BinaryWriter I override all the Write methods...
0
by: Shoveler | last post by:
I've got an odd problem here that I've been beating my head on for days. I've written a class that uses a pre-established connection for communication, meaning I can use this class for a server or...
0
by: fiefie.niles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message while...
9
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my...
12
by: Justin | last post by:
I can attach my code if anyone wants to see it however I'll try to ask my question with some mark up code first. I'm having a problem terminating my process while using DoEvents. For example: ...
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
1
by: ccarter45 | last post by:
I'm new to java and writing a program that accepts user input for a password and it has to meet the following requirements: 1. At least 6 characters long. 2. Leading character can't be a digit....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.