python跳出多重循环

来源:互联网 发布:土豪金手机淘宝 编辑:程序博客网 时间:2024/05/17 07:35
使用自定义异常可以跳出深层嵌套循环、看我做过的例子:class FoundException(Exception): passtry:    for row,record in enumerate(table):         for columu,field in enumerate(record):               for index,item in enumerate(field):                       if item == target:                                 raise FoundException()except FoundException:      print ("found at ({0},{1},{2})".format(row,column,index))else:       print ('not found')
封装成 函数   将 break 语句 换成  returndef test():    for x in range(9,0,-1):         for y in range(9,-1,-1):                for z in range(9,-1,-1):                   s = 100001*x+10010*y+1100*z                   s1 = int((round((s**0.5),0))) + 1                   for i in range(s1,100,-1):                      if s % s1 ==0:                         print s,i                         return s