【发布时间】:2022-01-20 11:16:33
【问题描述】:
如果条件满足,我如何编写 Python 脚本以将 whiles 与 ifs 和 break 整个嵌套循环结合起来?
我已阅读其他主题,但无法使脚本正常工作...
当满足if 条件时,如何编写此脚本以退出或中断所有嵌套循环?
脚本:
breaker = False
while True:
...commands...
if ... :
...commands...
if ... :
...commands...
while True:
...commands...
if ... :
if ... :
breaker = True
break # ...to exit all loops
i += 1
j -= 1
if breaker:
break
...continue script here if `break`
【问题讨论】:
-
将所有这些放在一个函数中,然后执行
return。 -
甚至在多个函数中。如此嵌套的代码很难阅读或推理。
-
如果我用函数和返回来写这个,会正常工作吗?
标签: python if-statement while-loop nested-loops break