943,549 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2509
  • Python RSS
Sep 12th, 2009
0

While and raw_input

Expand Post »
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.
python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
nunos is offline Offline
46 posts
since Aug 2009
Sep 12th, 2009
1

Re: While and raw_input

You are creating a situation where the while loop exit condition is always True. Test this ...
python Syntax (Toggle Plain Text)
  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 ...
python Syntax (Toggle Plain Text)
  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 ...
python Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Sep 12th, 2009
1

Re: While and raw_input

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.
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is online now Online
2,302 posts
since Dec 2006
Sep 13th, 2009
1

Re: While and raw_input

What woooee said was correct:
python Syntax (Toggle Plain Text)
  1. overwrite=""
  2. while(overwrite!="Y".lower() and overwrite!="N".lower()):
  3. overwrite=raw_input()
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Sep 13th, 2009
0

Re: While and raw_input

Thanks vegaseat, woooee and sravan953. You were all very helpful.
Reputation Points: 10
Solved Threads: 0
Light Poster
nunos is offline Offline
46 posts
since Aug 2009
Sep 13th, 2009
0

Re: While and raw_input

Click to Expand / Collapse  Quote originally posted by sravan953 ...
What woooee said was correct:
python Syntax (Toggle Plain Text)
  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'
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Sep 13th, 2009
0

Re: While and raw_input

@sneekula, that is what nunos wanted.
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Python Turtle module, and math module problem.
Next Thread in Python Forum Timeline: Function with variable number of parameters





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC