3 回答

TA貢獻1828條經驗 獲得超13個贊
使用標志通知外部斷路器:
while ..
for ..
should_break = False
for ..
if condition == True:
should_break = True
break
if should_break: break

TA貢獻1811條經驗 獲得超6個贊
不幸的是,我將用于 C 編程的解決方案,一個goto命令(討厭的人會討厭),在 Python 中不存在。
我也遇到過這種情況,我知道的最佳解決方案如下:
while ...
breakLoop = False
for ...:
for ...:
if condition:
breakLoop = True
break
if breakLoop:
break

TA貢獻1869條經驗 獲得超4個贊
我會嘗試重構它,這樣你就沒有那么多嵌套循環了。也許將內部循環移動到一個函數中?否則,您也可以在內循環之后添加中斷。
while ..
for ..
for ..
if condition == True:
break
if condition == True:
break
添加回答
舉報