# -*- coding: utf-8 -*- def triangles(): n = 0 L = [1] while n < 20: yield L L = [1] + [L[i] + L[i+1] for i in range(n)] + [1] # n =0 时,循环语句【L[i] + L[i+1]】根本不会执行,所以不会报错 n = n + 1 # 打印 for i in triangles(): print(i)
# -*- coding: utf-8 -*- def triangles(): n = 0 L = [1] while n < 20: yield L L = [1] + [L[i] + L[i+1] for i in range(n)] + [1] # n =0 时,循环语句【L[i] + L[i+1]】根本不会执行,所以不会报错 n = n + 1 # 打印 for i in triangles(): print(i)