#!/usr/bin/python3
#杨辉三角(只打印6层)
# 1
# 1 1
# 1 2 1
# 1 3 3 1
# 1 4 6 4 1
# 1 5 10 10 5 1'
#循环:
def Pascals_triangle(max):
t=[1]
n=1
while n<=max:
s=" ".join(str(t))
print(s.center(40))
L=t.copy()
for i in range(1,len(t)):
L[i]=t[i]+t[i-1]
L.append(1)
t=L
n+=1
Pascals_triangle(6)
# 生成器:
def Pascals_triangle(n):
l=[1]
while True:
s=" ".join(str(l))
yield(s.center(40))
l=[l[x]+l[x+1] for x in range(len(l)-1)]
l[0:0]=[1]
l.append(1)
if len(l)>n:
break
gen=Pascals_triangle(6)
it=iter(gen)
while True:
try:
x=next(it)
print(x)
except StopIteration:
break