杨辉三角

杨辉三角,python基础实现

 1 def triangle(floor):
 2     leng = floor * 6
 3     print("1".center(leng," "))
 4     print("1 1".center(leng," "))
 5     list_up = ["1", "1"]
 6     for i in range(3,floor+1):#i为每一层,到第n层
 7         list_lower= ["1","1"]#构建第i层
 8         for j in range(i - 2):
 9             list_lower.insert(1,None)#将列表扩长,然后填入None,后面将None换为应该填入的元素
10         for k in range(len(list_lower)):
11             if list_lower[k] == None:
12                 list_lower[k] = str(int(list_up[k]) + int(list_up[k-1]))
13                 len2 = len(list_lower)
14         list_up = list_lower.copy()
15         s_lower = (" ".join(list_lower)).center(leng," ")
16         print(s_lower)
17     return None

 

posted @ 2018-08-23 16:23  dmyHero  阅读(216)  评论(0编辑  收藏  举报