Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在我的代码工作时打破for并尝试循环?_Python_For Loop_While Loop_Try Catch - Fatal编程技术网

Python 如何在我的代码工作时打破for并尝试循环?

Python 如何在我的代码工作时打破for并尝试循环?,python,for-loop,while-loop,try-catch,Python,For Loop,While Loop,Try Catch,我有一个try循环,它执行一个post函数,这需要一些时间: def recalc_proj(proj): for x in range(0,10): while True: try: newproj = proj.post('docs/recalc') except someError: print "another job is running. waiti

我有一个try循环,它执行一个post函数,这需要一些时间:

def recalc_proj(proj):
    for x in range(0,10):
        while True:
            try:
                newproj = proj.post('docs/recalc')
            except someError:
                print "another job is running. waiting 10s"
                time.sleep(10)
                continue
            break
        print "SUCCESS"
我只想运行一次
newproj=proj.post('docs/recalc')

但是,在运行上述函数后,我在屏幕上得到以下输出:

another job is running. waiting 10s
another job is running. waiting 10s
another job is running. waiting 10s
another job is running. waiting 10s
SUCCESS
another job is running. waiting 10s
another job is running. waiting 10s
another job is running. waiting 10s

为什么即使在我需要的代码行成功运行之后,它仍会继续循环?我认为break意味着退出for循环?

语句终止当前循环,即示例中的
while
循环。您也可以在
print
语句之后放置另一个
break
,以终止
for
循环:

 ...
 print "SUCCESS"
 break