假设一段python程序有多重循环,我们都知道在一个循环当中,用break是退出当前的循环,然后继续下一次循环,但是如何才能跳出多重循环呢,实际就是结束所有的循环。
思路1::可以定义一个异常类,在需要退出循环的位置,跑出这个异常,就可以了,因为程序在遇到异常的时候,如果没有捕获异常的话,就是直接退出程序的执行,利用这个原理,认为的触发一个异常,然后把这个异常捕获到,就可以实现退出多重循环,而且不结束程序,继续往下执行
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#encoding=utf-8
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()#抛出一个异常,就会跳出所有循环
else:
print i,'---',j,'---',k
except getOutOfLoop:
pass
print "done"
结果:可以看到在循环到特定条件时,直接抛出异常,然后进行捕获,使程序跳出循环后并不结束,而是接着捕获异常的位置后面继续执行。
D:\>python test.py
0 --- 0 --- 0
0 --- 0 --- 1
0 --- 0 --- 2
0 --- 0 --- 3
0 --- 0 --- 4
0 --- 1 --- 0
0 --- 1 --- 1
0 --- 1 --- 2
0 --- 1 --- 3
0 --- 1 --- 4
0 --- 2 --- 0
0 --- 2 --- 1
0 --- 2 --- 2
0 --- 2 --- 3
0 --- 2 --- 4
0 --- 3 --- 0
0 --- 3 --- 1
0 --- 3 --- 2
0 --- 3 --- 3
0 --- 3 --- 4
0 --- 4 --- 0
0 --- 4 --- 1
0 --- 4 --- 2
0 --- 4 --- 3
0 --- 4 --- 4
1 --- 0 --- 0
1 --- 0 --- 1
1 --- 0 --- 2
1 --- 0 --- 3
1 --- 0 --- 4
1 --- 1 --- 0
1 --- 1 --- 1
1 --- 1 --- 2
1 --- 1 --- 3
1 --- 1 --- 4
1 --- 2 --- 0
1 --- 2 --- 1
1 --- 2 --- 2
1 --- 2 --- 3
1 --- 2 --- 4
1 --- 3 --- 0
1 --- 3 --- 1
1 --- 3 --- 2
1 --- 3 --- 3
1 --- 3 --- 4
1 --- 4 --- 0
1 --- 4 --- 1
1 --- 4 --- 2
1 --- 4 --- 3
1 --- 4 --- 4
2 --- 0 --- 0
2 --- 0 --- 1
2 --- 0 --- 2
2 --- 0 --- 3
2 --- 0 --- 4
2 --- 1 --- 0
2 --- 1 --- 1
2 --- 1 --- 2
2 --- 1 --- 3
2 --- 1 --- 4
2 --- 2 --- 0
2 --- 2 --- 1
2 --- 2 --- 2
2 --- 2 --- 3
2 --- 2 --- 4
2 --- 3 --- 0
2 --- 3 --- 1
2 --- 3 --- 2
2 --- 3 --- 3
2 --- 3 --- 4
2 --- 4 --- 0
2 --- 4 --- 1
2 --- 4 --- 2
2 --- 4 --- 3
2 --- 4 --- 4
3 --- 0 --- 0
3 --- 0 --- 1
3 --- 0 --- 2
3 --- 0 --- 3
3 --- 0 --- 4
3 --- 1 --- 0
3 --- 1 --- 1
3 --- 1 --- 2
3 --- 1 --- 3
3 --- 1 --- 4
3 --- 2 --- 0
3 --- 2 --- 1
3 --- 2 --- 2
3 --- 2 --- 3
3 --- 2 --- 4
3 --- 3 --- 0
3 --- 3 --- 1
3 --- 3 --- 2
done
思路2:在多重循环中用return,直接进行返回,程序遇到return就会认为程序执行结束,进而退出多个循环,这种情况,执行return后,程序不会继续往下执行,而是直接结束程序
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#encoding=utf-8
#函数中,用return会退出多重循环
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()
结果:
D:\>python test.py
0 --- 0 --- 0
0 --- 0 --- 1
0 --- 0 --- 2
0 --- 0 --- 3
0 --- 0 --- 4
0 --- 1 --- 0
0 --- 1 --- 1
0 --- 1 --- 2
0 --- 1 --- 3
0 --- 1 --- 4
0 --- 2 --- 0
0 --- 2 --- 1
0 --- 2 --- 2
0 --- 2 --- 3
0 --- 2 --- 4
0 --- 3 --- 0
0 --- 3 --- 1
0 --- 3 --- 2
0 --- 3 --- 3
0 --- 3 --- 4
0 --- 4 --- 0
0 --- 4 --- 1
0 --- 4 --- 2
0 --- 4 --- 3
0 --- 4 --- 4
1 --- 0 --- 0
1 --- 0 --- 1
1 --- 0 --- 2
1 --- 0 --- 3
1 --- 0 --- 4
1 --- 1 --- 0
1 --- 1 --- 1
1 --- 1 --- 2
1 --- 1 --- 3
1 --- 1 --- 4
1 --- 2 --- 0
1 --- 2 --- 1
1 --- 2 --- 2
1 --- 2 --- 3
1 --- 2 --- 4
1 --- 3 --- 0
1 --- 3 --- 1
1 --- 3 --- 2
1 --- 3 --- 3
1 --- 3 --- 4
1 --- 4 --- 0
1 --- 4 --- 1
1 --- 4 --- 2
1 --- 4 --- 3
1 --- 4 --- 4
2 --- 0 --- 0
2 --- 0 --- 1
2 --- 0 --- 2
2 --- 0 --- 3
2 --- 0 --- 4
2 --- 1 --- 0
2 --- 1 --- 1
2 --- 1 --- 2
2 --- 1 --- 3
2 --- 1 --- 4
2 --- 2 --- 0
2 --- 2 --- 1
2 --- 2 --- 2
2 --- 2 --- 3
2 --- 2 --- 4
2 --- 3 --- 0
2 --- 3 --- 1
2 --- 3 --- 2
2 --- 3 --- 3
2 --- 3 --- 4
2 --- 4 --- 0
2 --- 4 --- 1
2 --- 4 --- 2
2 --- 4 --- 3
2 --- 4 --- 4
3 --- 0 --- 0
3 --- 0 --- 1
3 --- 0 --- 2
3 --- 0 --- 3
3 --- 0 --- 4
3 --- 1 --- 0
3 --- 1 --- 1
3 --- 1 --- 2
3 --- 1 --- 3
3 --- 1 --- 4
3 --- 2 --- 0
3 --- 2 --- 1
3 --- 2 --- 2
3 --- 2 --- 3
3 --- 2 --- 4
3 --- 3 --- 0
3 --- 3 --- 1
3 --- 3 --- 2
网友评论