Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: restarting a loop

Tags:

python

loops

i have:

for i in range(2,n):
    if(something):
       do something
    else:
       do something else
       i = 2 **restart the loop

But that doesn't seem to work. Is there a way to restart that loop?

Thanks

like image 685
cbrulak Avatar asked Jan 29 '09 18:01

cbrulak


People also ask

Can you restart a loop in Python?

You can reset the value of the loop variable to i = 0 to restart the loop as soon as the user types in 'r' . You use the Python built-in input() function to take the user input in each iteration and return it as a string.

How do you start a for loop again in Python?

The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops.

How do you redo an iteration in Python?

(The "redo" statement is a statement that (just like "break" or "continue") affects looping behaviour - it jumps at the beginning of innermost loop and starts executing it again.)

How do I start a while loop again?

You use the continue statement to restart a loop such as the while loop, for loop or for-in loop. If there are nested loops, the continue statement will restart the innermost loop.


3 Answers

You may want to consider using a different type of loop where that logic is applicable, because it is the most obvious answer.

perhaps a:

i=2
while i < n:
    if something:
       do something
       i += 1
    else: 
       do something else  
       i = 2 #restart the loop  
like image 60
gnomed Avatar answered Oct 25 '22 09:10

gnomed


Changing the index variable i from within the loop is unlikely to do what you expect. You may need to use a while loop instead, and control the incrementing of the loop variable yourself. Each time around the for loop, i is reassigned with the next value from range(). So something like:

i = 2
while i < n:
    if(something):
        do something
    else:
        do something else
        i = 2 # restart the loop
        continue
    i += 1

In my example, the continue statement jumps back up to the top of the loop, skipping the i += 1 statement for that iteration. Otherwise, i is incremented as you would expect (same as the for loop).

like image 26
Greg Hewgill Avatar answered Oct 25 '22 10:10

Greg Hewgill


Here is an example using a generator's send() method:

def restartable(seq):
    while True:
        for item in seq:
            restart = yield item
            if restart:
                break
        else:
            raise StopIteration

Example Usage:

x = [1, 2, 3, 4, 5]
total = 0
r = restartable(x)
for item in r:
    if item == 5 and total < 100:
        total += r.send(True) 
    else:
        total += item
like image 4
Joel Cornett Avatar answered Oct 25 '22 09:10

Joel Cornett