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
Answer from Tom on Stack OverflowI 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
Here's a very simple way to emulate a do-while loop:
condition = True
while condition:
# loop body here
condition = test_loop_condition()
# end of loop
The key features of a do-while loop are that the loop body always executes at least once, and that the condition is evaluated at the bottom of the loop body. The control structure show here accomplishes both of these with no need for exceptions or break statements. It does introduce one extra Boolean variable.
As a beginner how do I understand while loops?
How to learn for loop, do while loop and functions?
While loop help
You're very close, you just have line 13 indented one to many times.
def choosedoor():More on reddit.com
door = ''
while door != '1' and door != '2':
print ("Which door do you choose? (1 or 2)")
door = input()
return door
While loop not breaking.
I don't see exactly what's wrong, but maybe it's because you've only given us the gist. However if you're looking for breaking on a specific condition, that's exactly what the while loop condition is for. I'd try something like this:
marker = NoneMore on reddit.com
while marker not in ['X','O']:
marker = input("Select Marker 'X' or 'O' ").upper()
if marker == 'X':
...
Videos
While loops is kinda frustrating I'm 20 days into python and I'm stuck on loops since last 4 days
I work as an Analyst, have some experience with Python, like using groupby, filter conditions, joins, windows function, rank function to basically get the business logic to code. I see people who are efficient in coding tend to write their code as a single function rather than going through each step one at a time like cells in jupyter notebook. I want to learn how can I be better at writing loops, conditions and functions together as a whole. Like for example my coworker built a function where he declared a empty list outside the function, use it within, used counters and basically ran a groupby with agg to include the function fetching the desired output as a dataframe. How will I be able to work on such complex functions using loops and other parameters?