Python 本身没有“break n” 和“goto” 的语法,这也造成了Python 难以跳出多层(特定层数)循环.下面是几个跳出多层(特定层数)循环的tip. 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() else: print i, '-
Alex大神的需求:三层循环,在最内层循环中使用break,让所有循环结束; # 编辑者:闫龙 i=1; count=0; while 1==i : while 1==i: while 1==i: count+=1; print("我循环了",count,"次"); if count == 5: print("我要退出了"); i = 10000; break; print("所有循环已跳出"); 说实话,这个东西特么的,还真