Discuss / Python / 求助:完全不知道为什么有两行[1,1]

求助:完全不知道为什么有两行[1,1]

Topic source

def triangles(max): yield [1] L=[1,1] n=0 while n<max-2: yield L L=[1]+[L[i]+L[i+1] for i in range(n)]+[1] n=n+1 for n in triangles(10): print(n)

[1] [1, 1] [1, 1] [1, 2, 1] [1, 3, 3, 1] [1, 4, 6, 4, 1] [1, 5, 10, 10, 5, 1] [1, 6, 15, 20, 15, 6, 1] [1, 7, 21, 35, 35, 21, 7, 1]

def triangles(max): yield [1] L=[1,1] n=0 while n<max-2: L=[1]+[L[i]+L[i+1] for i in range(n)]+[1] n=n+1 yield L

再试试

n=0 改成 n=1 应该就行了


  • 1

Reply