我有一个名为evaluate 的函数,它是一个需要时间才能完成的函数。然后它继续一个循环,跳过那次迭代:
我的代码:
array = []#some huge array containing different ways of expressing greetings
def main(resumeLocation):
for a in range(len(array)):
i = array[a]
if a < resumeLocation:
continue
else:
if (i == "Hello")
answer(i, a)
break
def answer(input, resumeLocation):
# process answer
resumeLoop(resumeLocation)
现在,为了使函数不无限循环,我需要跳过处理答案的迭代,所以我需要打破循环,调用该函数,然后恢复循环,但是,我似乎无法弄清楚如何做到这一点。
任何建议都是有帮助的。
谢谢
请您参考如下方法:
正如一些评论所提到的,我相信有更好的方法可以做到这一点,但如果你真的想从中断的地方开始循环,你可以尝试这种方法:
arr = [1,2,3,4,5]
def answer(x, y, arr):
print('About to restart the loop at index: ', x+1)
loop(x+1, arr)
def loop(i, arr):
for x in range(i, len(arr)):
t = arr[x]
print(t)
if t == 3:
answer(x, t, arr)
break
loop(0, arr)
当满足条件时,调用answer() 并中断循环,但保留当前索引,然后当answer 完成时,函数是使用正确的起始索引调用。这段代码的输出如下:
1
2
3
About to restart the loop at index: 3
4
5
它在下一个索引处正确地重新启动循环。
