python 跳出巢狀迴圈方法

weixin_34344677發表於2013-07-25
class LoopError(Exception):pass

rs = ''
try:
    for i in range(1, 3):
        print i
        rs = 'one '
        if i == 1:
            for j in range(12, 22):
                print j
                if j == 13:
                    rs = 'bbs'
                    raise LoopError
except LoopError: #跳出,遇到LoopError
    print rs
    print 'good'
else: #迴圈完畢,沒丟擲LoopError
    print 'Not found'

 

相關文章