Python doesn't provide an easy way to get out of multiple loops. You can prepare a flag, but it's not very beautiful
python
flag = False
for i in range(100):
for j in range(100):
if i > j > 70:
flag = True
break
print i, j
if flag:
break
I think that such a for loop can be reviewed from the structure. There are times when I want you to break out of multiple loops. In such a case
python
for i in range(100):
for j in range(100):
if i > j > 70:
break
print i, j
else:
continue
break
Not so beautiful
python
try:
for i in range(100):
for j in range(100):
if i > j > 70:
raise Exception
print i, j
except Exception:
pass
It's different from the original usage and I can't really like it ~
python
from goto import goto, label
for i in range(100):
for j in range(100):
if i > j > 70:
goto .END
print i, j
label .END
Easy to understand but requires installation of external modules
All of them are not good enough, and they should be made into functions. Yes.
Recommended Posts