473,385 Members | 1,320 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,385 software developers and data experts.

While loop with "or"? Please help!

Hmm, my while loop with "or" doesn't seem to work as I want it to...
How do I tell the while loop to only accept "Y" or "y" or "N" or "n"
input from the str(raw_input)?

Thank's in advance!

Snippet of code:

import os

def buildfinder():
os.system("CLS")
GameRoot = os.getenv("GAME_ROOT") + "\\"

print "Do you want to use " + GameRoot + " as your source
directory?"
usr = str(raw_input('Y/N: '))
return usr

#Runs the buildfinder function
usrinp = buildfinder()

def buildwhiler():

while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
print "Enter Y or N!"
usr = str(raw_input('Y/N: '))
else:
code continues

Jan 25 '07 #1
10 2054
while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
Correct way:
while usrinp != "y" or usrinp != "Y" or usrinp != "N" or usrinp != "n":
There has to be a boolean evaluation on both sides of or.

Or in this case:
while usrinp not in ['Y', 'y', 'N', 'n']:

Ravi Teja.

Jan 25 '07 #2
wd********@gmail.com a écrit :
Hmm, my while loop with "or" doesn't seem to work as I want it to...
How do I tell the while loop to only accept "Y" or "y" or "N" or "n"
input from the str(raw_input)?

Thank's in advance!

Snippet of code:

import os

def buildfinder():
os.system("CLS")
GameRoot = os.getenv("GAME_ROOT") + "\\"

print "Do you want to use " + GameRoot + " as your source
directory?"
usr = str(raw_input('Y/N: '))
return usr

#Runs the buildfinder function
usrinp = buildfinder()

def buildwhiler():

while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
Yes, obviously.

The above reads as:
while (usrinp != "y") or ("Y") or ("N") or ("n"):

What you want is something like:
while usrinp != "y" and usrinp != "Y" \
and usrinp != "n" and usrinp != "N" :

which is better written as
while not (usrinp == "y" or usrinp == "Y" \
or usrinp == "n" or usrinp == "N") :

which can be simplified using str.lower:
while not (usrinp.lower() == "y" or usrinp.lower() == "n"):

and simplified again thanks to Python 'in' operator:
while not usrinp.lower() in "yn":

print "Enter Y or N!"
usr = str(raw_input('Y/N: '))
else:
code continues

While we're at it, there are other problems in your code. One of them is
that buildwhiler() will go in an infinite loop if the usr first typed
anything else than "y", "Y", "n" or "N". I let you try to find out why,
and just give you two hints :
- globals are Bad(tm)
- don't mix application logic with user-interface logic (ie: a function
should only deal with one or the other, not mix both).

HTH
Jan 25 '07 #3
wd********@gmail.com writes:
while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
while userinp not in 'yYnN':
...

Or maybe more generally:

while userinp.lower() not in 'yn': # case independent test
...
Jan 25 '07 #4
Bruno Desthuilliers wrote:
and simplified again thanks to Python 'in' operator:
while*not*usrinp.lower()*in*"yn":
But note that 'in' performs a substring search and therefore "yn" and ""
would be accepted as valid answers, too.

Peter

Jan 25 '07 #5
Peter Otten a écrit :
Bruno Desthuilliers wrote:
>and simplified again thanks to Python 'in' operator:
while not usrinp.lower() in "yn":

But note that 'in' performs a substring search and therefore "yn" and ""
would be accepted as valid answers, too.
Mmm, right. Thanks for the correction.

=>
while not usrinp.lower() in ['y', 'n']:

Jan 25 '07 #6
On 25/01/07, Bruno Desthuilliers <br*****************@websiteburo.comwrote:
Peter Otten a écrit :
Bruno Desthuilliers wrote:
and simplified again thanks to Python 'in' operator:
while not usrinp.lower() in "yn":
But note that 'in' performs a substring search and therefore "yn" and ""
would be accepted as valid answers, too.

Mmm, right. Thanks for the correction.

=>
while not usrinp.lower() in ['y', 'n']:
or better still

while usrinp.lower() not in ['y', 'n']:

:):)
--

Tim Williams
Jan 25 '07 #7
Peter Otten <__*******@web.dewrites:
while*not*usrinp.lower()*in*"yn":

But note that 'in' performs a substring search and therefore "yn" and ""
would be accepted as valid answers, too.
Oh right, that's a recent change to the language, I think. OK:

while*not*usrinp.lower()*in*list("yn"): ...
Jan 25 '07 #8


On Jan 25, 11:26 am, wd.jons...@gmail.com wrote:
Hmm, my while loop with "or" doesn't seem to work as I want it to...
How do I tell the while loop to only accept "Y" or "y" or "N" or "n"
input from the str(raw_input)?

Thank's in advance!

Snippet of code:

import os

def buildfinder():
os.system("CLS")
GameRoot = os.getenv("GAME_ROOT") + "\\"

print "Do you want to use " + GameRoot + " as your source
directory?"
usr = str(raw_input('Y/N: '))
return usr

#Runs the buildfinder function
usrinp = buildfinder()

def buildwhiler():

while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
print "Enter Y or N!"
usr = str(raw_input('Y/N: '))
else:
code continues
also note that your naming of variables contains two different names
for one variable...

better

usrinp = ''
while usrinp.lower() not in ('y', 'n'):
usrinp = raw_input('Y/N: ') # and not usr....

Jan 25 '07 #9
wi******@hotmail.com a écrit :
>
On Jan 25, 11:26 am, wd.jons...@gmail.com wrote:
(snip)
>#Runs the buildfinder function
usrinp = buildfinder()

def buildwhiler():

while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
print "Enter Y or N!"
usr = str(raw_input('Y/N: '))
else:
code continues

also note that your naming of variables contains two different names
for one variable...
Actually, usrinp and usr are really two different vars - the first one
being global...

And FWIW, it's one of the "other errors" I mentionned earlier <g>

Jan 25 '07 #10
2007-01-25 11:26:09
wd********@gmail.com wrote in message
<11*********************@j27g2000cwj.googlegroups. com>
Hmm, my while loop with "or" doesn't seem to work as I want it to...
How do I tell the while loop to only accept "Y" or "y" or "N" or
"n"
input from the str(raw_input)?

Thank's in advance!

Snippet of code:

import os

def buildfinder():
os.system("CLS")
GameRoot = os.getenv("GAME_ROOT") + "\\"

print "Do you want to use " + GameRoot + " as your source
directory?"
usr = str(raw_input('Y/N: '))
return usr

#Runs the buildfinder function
usrinp = buildfinder()

def buildwhiler():

while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
print "Enter Y or N!"
usr = str(raw_input('Y/N: '))
else:
code continues
Thanks for the help everyone! :)
This forum rules!
Jan 25 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
28
by: john_sips_tea | last post by:
Just tried Ruby over the past two days. I won't bore you with the reasons I didn't like it, however one thing really struck me about it that I think we (the Python community) can learn from. ...
1
by: Appu | last post by:
How to Check in the window print dialog box whether we clicked either "print" or "cancel". while clicking a button i call wnidow.print() to pop up the windows PRint Dialog box. I want to check...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
43
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.