Ad

In A For Loop How To Send The I Few Loops Back Upon A Condition

- 1 answer

is there any way to send the value of a for look back in few loops..

for i in range(10):
    print(i)
    if random.randint(0,9) in [5,6]:
        i = 0

what i want to do is exactly something like this. now here i know the range(10) generates the range [0,1,2,3,4,5,6,7,8,9] first the then traverse it by assigning the value to i. so simply changing the value of i will not work.

but is there any other way to change i in a way that the value in the next loop will i+1

Ad

Answer

This will work

import random
i = 0
while i < 10:
    print(i)
    if random.randint(0,9) in [5,6]:
        i = 0
    else:
        i +=1

Edit: my bad, it won't work indeed, while loop will.

Ad
source: stackoverflow.com
Ad