python: restarting a loop

后端 未结 5 743
日久生厌
日久生厌 2020-11-30 08:50

i have:

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

But

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 09:24

    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  
    

提交回复
热议问题