用户:
iops查看:0 回复:0 评论:0 创建时间:2022-08-13T11:49:23
【作品展示】

【作品介绍】
杨辉金字塔
【作品源代码】
'''杨辉三角金字塔v1.0-工程包'''
a = input('行数:')
res = []
#绘制前九行的杨辉三角列表
for x in range(int(a)):
#前两行,分别为[1], [1, 1]
if x == 0:
res.append([1])
elif x == 1:
res.append([1, 1])
#从第三行起,根据上一行确定本行内容
else:
#定义一个列表l,存放每行的数据
l = []
for y in range(len(res[x-1])):
if y == 0:
l.append(1)
else:
l.append(res[x - 1][y] + res[x - 1][y - 1])
l.append(1)
#每次循环把列表l添加到res列表中
res.append(l)
#打印前九行杨辉三角列表
for x in res:
ret = ''
for y in x:
ret += (str(y) + ' ')
print(ret.center(60))
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn