| | |
While and raw_input
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
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,068
Reputation:
Solved Threads: 299
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
Views: 662 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for python, raw_input
abrupt address advanced advice ajax anti apax avogadro backend beginner bluetooth c++ calculator code coordinates copy curved curves def edit event examples excel file filename function google gui hints homework http images infosec input ip jaunty java keyboard launcher linux list lists loop maze module movingimageswithpygame mysqldb newb output parameters path program programming projects push py2exe pygame pyglet pyqt python rails random recursion recursive return ruby rubyconf server shebang silverlight skinning slicenotation smtp software source sqlite ssh string strings strip sum table tennis text threading tkinter tlapse tricks tutorial ubuntu url urllib urllib2 variable ventrilo wikipedia wordgame write wxpython xlwt







