While and raw_input

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 35
Reputation: nunos is an unknown quantity at this point 
Solved Threads: 0
nunos nunos is offline Offline
Light Poster

While and raw_input

 
0
  #1
Sep 12th, 2009
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.
  1. overwrite = None
  2. while overwrite != 'Y'.lower() or overwrite != 'N'.lower():
  3. overwrite = raw_input("Do you want to overwrite (Y/N)? ")

It doesn't exit the while loop.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,137
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 946
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: While and raw_input

 
1
  #2
Sep 12th, 2009
You are creating a situation where the while loop exit condition is always True. Test this ...
  1. overwrite = None
  2. while overwrite != 'y' or overwrite != 'n':
  3. overwrite = raw_input("Do you want to overwrite (Y/N)? ").lower()
  4. print overwrite != 'y'
  5. print overwrite != 'n'
  6. # overall condition
  7. print False or True
Easier to understand is this approach ...
  1. while True:
  2. overwrite = raw_input("Do you want to overwrite (Y/N)? ").lower()
  3. if overwrite == 'y' or overwrite == 'n':
  4. break
... or even simpler ...
  1. while True:
  2. overwrite = raw_input("Do you want to overwrite (Y/N)? ").lower()
  3. if overwrite in 'yn':
  4. break
Last edited by vegaseat; Sep 12th, 2009 at 3:49 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,068
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is online now Online
Veteran Poster

Re: While and raw_input

 
1
  #3
Sep 12th, 2009
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.
  1. while overwrite != 'Y'.lower() or overwrite != 'N'.lower(): becomes
  2. if overwrite != 'Y'.lower():
  3. do something
  4. elif overwrite != 'N'.lower():
  5. do the same thing
  6.  
  7. For completeness, the 'and' would be
  8. if overwrite != 'Y'.lower() and overwrite != 'N'.lower(): becomes
  9. if overwrite != 'Y'.lower():
  10. if overwrite != 'N'.lower():
  11. do something
Hopefully this will help sort out the and/or's in the future.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Re: While and raw_input

 
1
  #4
Sep 13th, 2009
What woooee said was correct:
  1. overwrite=""
  2. while(overwrite!="Y".lower() and overwrite!="N".lower()):
  3. overwrite=raw_input()
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 35
Reputation: nunos is an unknown quantity at this point 
Solved Threads: 0
nunos nunos is offline Offline
Light Poster

Re: While and raw_input

 
0
  #5
Sep 13th, 2009
Thanks vegaseat, woooee and sravan953. You were all very helpful.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: While and raw_input

 
0
  #6
Sep 13th, 2009
Originally Posted by sravan953 View Post
What woooee said was correct:
  1. overwrite=""
  2. while(overwrite!="Y".lower() and overwrite!="N".lower()):
  3. overwrite=raw_input()
Here "Y".lower() does not make any sense since it is simply 'y'
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Re: While and raw_input

 
0
  #7
Sep 13th, 2009
@sneekula, that is what nunos wanted.
Reply With Quote Quick reply to this message  
Reply

Tags
python, raw_input

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 662 | Replies: 6
Thread Tools Search this Thread



Tag cloud for python, raw_input
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC