qwerty ayyy
qwerty ayyy

Reputation: 385

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)

Upvotes: 3

Views: 1508

Answers (3)

yael
yael

Reputation: 337

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

Upvotes: 1

Paul Draper
Paul Draper

Reputation: 83255

You just need to add an infinite while loop that breaks at the end of the iteration.

Then you can restart the iteration as often as needed.

bad_order = []
order = []
for iteration in range(0, 10):
    while True: #
        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)

        break #

Upvotes: 0

Tom Karzes
Tom Karzes

Reputation: 24052

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

Upvotes: 4

Related Questions