How to emulate a do-while loop?
I am not sure what you are trying to do. You can implement a do-while loop like this:
while True:
stuff()
if fail_condition:
break
Or:
stuff()
while not fail_condition:
stuff()
What are you doing trying to use a do while loop to print the stuff in the list? Why not just use:
for i in l:
print i
print "done"
Update:
So do you have a list of lines? And you want to keep iterating through it? How about:
for s in l:
while True:
stuff()
# use a "break" instead of s = i.next()
Does that seem like something close to what you would want? With your code example, it would be:
for s in some_list:
while True:
if state is STATE_CODE:
if "//" in s:
tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
state = STATE_COMMENT
else :
tokens.add( TOKEN_CODE, s )
if state is STATE_COMMENT:
if "//" in s:
tokens.append( TOKEN_COMMENT, s.split( "//" )[1] )
break # get next s
else:
state = STATE_CODE
# re-evaluate same line
# continues automatically
how to emulate the do while statement in python?
Python doesn't have a do while loop, u can simulate it like this
i = 1
while True:
print(i)
i = i + 1
if(i > 100):
break
Emulating a do-while loop in Bash
Two simple solutions:
Execute your code once before the while loop
actions() {
check_if_file_present
# Do other stuff
}
actions #1st execution
while [ current_time <= $cutoff ]; do
actions # Loop execution
doneOr:
while : ; do
actions
[[ current_time <= $cutoff ]] || break
done
Why there is no do while loop in python
There is no do...while
loop because there is no nice way to define one that fits in the statement: indented block
pattern used by every other Python compound statement. As such proposals to add such syntax have never reached agreement.
Nor is there really any need to have such a construct, not when you can just do:
while True:
# statement(s)
if not condition:
break
and have the exact same effect as a C do { .. } while condition
loop.
See PEP 315 -- Enhanced While Loop:
Rejected [...] because no syntax emerged that could
compete with the following form:while True:
<setup code>
if not <condition>:
break
<loop body>
A syntax alternative to the one proposed in the PEP was found for
a basic do-while loop but it gained little support because the
condition was at the top:do ... while <cond>:
<loop body>
or, as Guido van Rossum put it:
Please reject the PEP. More variations along these lines won't make the
language more elegant or easier to learn. They'd just save a few hasty
folks some typing while making others who have to read/maintain their code
wonder what it means.
Why is my nested while loop not working? if and second while loop is expected
Try this
used = []
#First Loop
while True:
search = True #moved search = True here so that the nested loop will run each iteration of the main loop as it will be set back to True
pic = driver.find_element(By.CSS_SELECTOR,value=".image-post img")
time.sleep(2)
pic_url = pic.get_attribute("src") #you’re not doing anything with this from the looks of it
pic_title = pic.get_attribute("alt") #same with this
used.append(pic)
time.sleep(200)
#Second loop
while search:
pic = driver.find_element(By.CSS_SELECTOR, value=".image-post img")
if pic != used:#may want to change “used” to “used[-1]” here as used is a list while it appears pic is not, so the -1 will get the last value in used
search = False
used.append(pic)
You could also replace search with True and change the “search = False” with “break”
Related Topics
Running a Bash Script from Python
How to Cross Compile Python Interpreter for Windows Under Linux
Tutorial or Guide for Scripting Xcode Build Phases
Cannot Open Include File: 'Io.H': No Such File or Directory
CSV in Python Adding an Extra Carriage Return, on Windows
What Is the Fastest Way to Send 100,000 Http Requests in Python
How Accurate Is Python's Time.Sleep()
Open Document with Default Os Application in Python, Both in Windows and MAC Os
Typeerror: 'Int' Object Is Not Callable
Python Multiprocessing + Subprocess Issues
Detect Specific Keypresses in Gui
Can't Start Foreman in Heroku Tutorial Using Python
Security of Python's Eval() on Untrusted Strings
Removing Duplicates from a List of Lists
Understanding Dict.Copy() - Shallow or Deep
Converting a String Representation of a List into an Actual List Object
How to See the Entire Http Request That's Being Sent by My Python Application