Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart current iteration in Python

I want to restart the current iteration of the first for loop when testing12(bad_order, order) == True. I have tried to use continue but it skips over the iteration which is not what I want.

bad_order = []
order = []
for iteration in range(0, 10):

    args = []
    print("\n  def test%d(self):" % (iteration))

    for input in range(num_arguments):

        args.append(pick_type())
        order = args

    if testing12(bad_order, order) == True:
        continue

    try:
        result = target(*args)
        code = test_to_string(target, args, result)

    except Exception as error:
        bad_order = args
        code = test_to_string_exc(target, args, error)
like image 515
qwerty ayyy Avatar asked May 12 '26 16:05

qwerty ayyy


2 Answers

You can add an inner while loop which will in effect repeat the outer loop iteration until it exits. If you can put the repeat condition in the while test, then you're done:

for iteration in range(0, 10):
    while some_condition:
        ...

If not, you can use a while True loop, put an unconditional break at the bottom, and use a continue to repeat:

for iteration in range(0, 10):
    while True:
        ...
        if continue_condition:
            continue
        ...
        break
like image 109
Tom Karzes Avatar answered May 14 '26 08:05

Tom Karzes


you can also change the:

for iteration in range(0,10):

to

iteration =0 
while iteration < 10 :

and increment iteration only when no repeat is needed

like image 34
yael Avatar answered May 14 '26 07:05

yael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!