Python跳出多重循环的方法
http://blog.csdn.net/churximi/article/details/51043595
方法1:自定义异常
# -*- coding:utf-8 -*- """ 功能:python跳出循环 """ # 方法1:自定义异常 class Getoutofloop(Exception): pass try: for i in range(5): for j in range(5): if i == j == 2: raise Getoutofloop() else: print i, '----', j except Getoutofloop: pass
方法2:将循环封装为函数,return
方法3:用for...else...语句
# -*- coding:utf-8 -*- """ 功能:python跳出循环 """ # 方法2:for...else...用法,用于跳出指定循环层 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: # else1 continue break # break1 else: # else2 continue break # break2