| | |
While and raw_input
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 35
Reputation:
Solved Threads: 0
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.
It doesn't exit the while loop.
Thanks.
python Syntax (Toggle Plain Text)
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.
You are creating a situation where the while loop exit condition is always True. Test this ...
Easier to understand is this approach ...
... or even simpler ...
python Syntax (Toggle Plain Text)
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
python Syntax (Toggle Plain Text)
while True: overwrite = raw_input("Do you want to overwrite (Y/N)? ").lower() if overwrite == 'y' or overwrite == 'n': break
python Syntax (Toggle Plain Text)
while True: overwrite = raw_input("Do you want to overwrite (Y/N)? ").lower() if overwrite in 'yn': break
Last edited by vegaseat; Sep 12th, 2009 at 3:49 pm.
May 'the Google' be with you!
•
•
Join Date: Dec 2006
Posts: 1,017
Reputation:
Solved Threads: 286
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.
Hopefully this will help sort out the and/or's in the future.
Python Syntax (Toggle Plain Text)
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
Linux counter #99383
What woooee said was correct:
python Syntax (Toggle Plain Text)
overwrite="" while(overwrite!="Y".lower() and overwrite!="N".lower()): overwrite=raw_input()
•
•
•
•
What woooee said was correct:
python Syntax (Toggle Plain Text)
overwrite="" while(overwrite!="Y".lower() and overwrite!="N".lower()): overwrite=raw_input()
No one died when Clinton lied.
![]() |
Similar Threads
- Raw_input... (Python)
- how to use loop & time with raw_input (Python)
- Insert text in raw_input (Python)
- raw_input StartsWith and other questions (Python)
- raw_input exception (Python)
Other Threads in the Python Forum
- Previous Thread: Python Turtle module, and math module problem.
- Next Thread: Function with variable number of parameters
| Thread Tools | Search this Thread |
accessdenied ajax aliased arax array avogadro bash beginner book c++ calling class code console convert corners csv cturtle curved cx-freeze data development dictionary digital dynamic embed enter examples file function generator google gui halp homework ideas iframe input itunes java keycontrol leftmouse library line linux list loan microphone microsoft module mouse mysql obexftp opensource prime programming projects py2exe pygame pygtk pyopengl python random raw_input read recursive redirect remote return reverse ruby script simple skinning slicenotation sprite string sudokusolver syntax table tennis terminal text thread threading time tkinter tooltip tutorial ubuntu unicode url urllib urllib2 variable verify voip web-scrape whileloop wxpython







