954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

While and raw_input

Can someone please tell me what I am doing wrong. I know this is easy, but I haven't managed to make it work properly.

overwrite = None
while overwrite != 'Y'.lower() or overwrite != 'N'.lower():
    overwrite = raw_input("Do you want to overwrite (Y/N)? ")


It doesn't exit the while loop.

Thanks.

nunos
Light Poster
47 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

You are creating a situation where the while loop exit condition is always True. Test this ...

overwrite = None
while overwrite != 'y' or overwrite != 'n':
    overwrite = raw_input("Do you want to overwrite (Y/N)? ").lower()
    print overwrite != 'y'
    print overwrite != 'n'
    # overall condition
    print False or True

Easier to understand is this approach ...

while True:
    overwrite = raw_input("Do you want to overwrite (Y/N)? ").lower()
    if overwrite == 'y' or overwrite == 'n':
        break


... or even simpler ...

while True:
    overwrite = raw_input("Do you want to overwrite (Y/N)? ").lower()
    if overwrite in 'yn':
        break
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

For future coding conundrums, an "or" can be viewed as an if / elif, and "and" can be viewed as two nested if statements, which you would adapt to while() statements.

while overwrite != 'Y'.lower() or overwrite != 'N'.lower():  becomes
if overwrite != 'Y'.lower():
    do something
elif overwrite != 'N'.lower():
    do the same thing

For completeness, the 'and' would be
if overwrite != 'Y'.lower() and overwrite != 'N'.lower():  becomes
if overwrite != 'Y'.lower():
    if overwrite != 'N'.lower():
       do something


Hopefully this will help sort out the and/or's in the future.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

What woooee said was correct:

overwrite=""
while(overwrite!="Y".lower() and overwrite!="N".lower()):
    overwrite=raw_input()
sravan953
Posting Whiz in Training
243 posts since May 2009
Reputation Points: 2
Solved Threads: 30
 

Thanks vegaseat, woooee and sravan953. You were all very helpful. :icon_wink:

nunos
Light Poster
47 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

What woooee said was correct:

overwrite=""
while(overwrite!="Y".lower() and overwrite!="N".lower()):
    overwrite=raw_input()

Here "Y".lower() does not make any sense since it is simply 'y'

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

@sneekula, that is what nunos wanted.

sravan953
Posting Whiz in Training
243 posts since May 2009
Reputation Points: 2
Solved Threads: 30
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: