python:重新启动循环 [英] python: restarting a loop

查看:190
本文介绍了python:重新启动循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

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

但这似乎不起作用.有没有办法重新启动该循环?

But that doesn't seem to work. Is there a way to restart that loop?

谢谢

推荐答案

您可能要考虑在适用该逻辑的情况下使用其他类型的循环,因为这是最明显的答案.

You may want to consider using a different type of loop where that logic is applicable, because it is the most obvious answer.

也许是:

i=2
while i < n:
    if something:
       do something
       i += 1
    else: 
       do something else  
       i = 2 #restart the loop  

这篇关于python:重新启动循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆