Python3简单实现杨辉三角

def createL(l): # 生成杨辉三角的一行
    L = [1]
    for x in range(1, len(l)):
        L.append(l[x] + l[x-1])
    L.append(1)
    return L


def printL(L, W): # 打印
    s = ""
    for x in L:
        s += str(x) + "  "
    print(s.center(W))


L = [1]
row = int(input("输入行数:"))
width = row * 4 # 设置打印宽度
for x in range(row):
    printL(L, width)
    L = createL(L)

 输出结果为:(跟外面那些妖艳的代码不一样)

                《Python3简单实现杨辉三角》

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/zyz1431/article/details/79104035
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞