# 'break' works on the innermost loop. If we want to break out # of the outer loop, we often have to resort to flag variable. # Rewrite the following example to use .close() to stop the # outer loop. from __future__ import print_function def counter(n): i = 1 while i <= n: yield i i += 1 def print_table(): outer = counter(10) finished = False # <---- get rid of this total, limit = 0, 100 for i in outer: inner = counter(i) print(i, end=': ') for j in inner: print(i * j, end=' ') total += i * j if total >= limit: finished = True # <----- and this break print() if finished: # <----- and also this break # print('total:', total) if __name__ == '__main__': print_table()