您的位置:首页 > 编程语言 > Python开发

python3 简单实现杨辉三角

2018-03-22 16:20 190 查看
def yanghui():
L = [1]
while True:
yield L
if len(L)==1:
L.append(1)
else:
# for i in range(0,len(L)-1):
# temp[i] = L[i]+L[i+1]
temp = [L[i] +L[i+1] for i in range(0,len(L)-1)]
L = [1]+temp+[1]


测试代码:#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'a test module'

__author__ = 'Aran Li'

import def_yanghui

def testyh(n):
re= []
count = 0
for t in def_yanghui.yanghui():
print(t)
re.append(t)
count = count +1
if count == n:
break
print(re)

输出会出现[1]变成[1,1],排查不到原因
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: