Redo for loop iteration in Python

前端 未结 6 1431
灰色年华
灰色年华 2020-12-10 04:07

Does Python have anything in the fashion of a \"redo\" statement that exists in some languages?

(The \"redo\" statement is a statement that (just like \"break\" or \

6条回答
  •  一个人的身影
    2020-12-10 04:32

    Not very sofiscated but easy to read, using a while and an increment at the end of the loop. So any continue in between will have the effect of a redo. Sample to redo every multiple of 3:

    redo = True # To ends redo condition in this sample only
    i = 0
    while i<10:
       print(i, end='')
       if redo and i % 3 == 0:
          redo = False # To not loop indifinively in this sample
          continue # Redo
       redo = True
       i += 1
    

    Result: 00123345667899

提交回复
热议问题