- ‘‘‘方法1:跳出多重循环‘‘‘
class getoutofloop(Exception):
pass
try:
for i in range(5):
for j in range(5):
for k in range(5):
if i==j==k==3:
raise getoutofloop() #raise 抛出异常对象,退出多重循环,不会继续执行try中语句
else:
print(i,j,k)
except getoutofloop:
print("抛出异常对象")
print("done")
- ‘‘‘方法2:跳出多重循环‘‘‘
def test():
for i in range(5):
for j in range(5):
for k in range(5):
if i==j==k==3:
return
else:
print(i,j,k)
test() - ‘‘‘方法3:跳出多重循环‘‘‘
for i in range(5):
for j in range(5):
for k in range(5):
if i == j == k == 3:
break
else:
print(i,j,k)
else:continue
break
else:continue
break