代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName :杨辉三角.py
# @Time :2020/3/8 16:45
# @Author :anqixiang
# @Function :

low = 6 #打印多少行,需大于2
list1 = [1] #第一行
list2 = [1, 1] #第二行

def trigon(alist,n):
blist = [1]
if n > 0:
for j in range(len(alist)):
if j < len(alist) - 1:
sum = alist[j] + alist[j+1]
blist.append(sum)
blist.append(1)
print(str(blist).center(50))
n -= 1
trigon(blist, n)

if __name__ == "__main__":
print(str(list1).center(50))
print(str(list2).center(50))
trigon(list2,low - 2)
run_code = 0

效果

Python打印杨辉三角_python