python爬文章_python:简单爬取自己的一篇博客文章

本文介绍了如何使用Python实现杨辉三角形。通过定义一个生成器函数`yanghui()`,利用yield语句生成每行的元素,并使用循环打印指定行数的杨辉三角形。用户可以输入想要显示的行数。
摘要由CSDN通过智能技术生成

python实现杨辉三角形

python实现杨辉三角形- Mr_choa -博客园

var currentBlogApp= 'Mr-choa';

var cb_enable_mathjax=false;

var isLogined=false;

var skinName= 'CodingLife';

python实现杨辉三角形

代码实现:
# python实现杨辉三角形

defyanghui():# 定义第一行列表为[1]

line = [1]whileTrue:# yield的作用:把一个函数变成生成器,同时返回一个list,下次从yield的下条语句执行

yieldline# 设上一个为[1],通过式子可得[1,1],继而[1,2,1]......

line = [1] + [line[i] + line[i + 1] for i in range(len(line) - 1)] + [1]# 输入杨辉三角形的行数

n = int(input("请输入行数:"))

# 定义一个结束的变量

flag =0# 生成器可迭代,做个遍历

for i inyanghui():# 打印每行的列表的元素,用空格连接

print(" ".join(str(j) for j in i))

# 打印完一行,flag+1

flag += 1

# 如果变量flag等于输入的行数,跳出for循环

if flag ==n:# 跳出循环

break

效果:

请输入行数:6

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

Process finished with exit code 0

 
         
posted @

2020-03-14 23:23

Mr_choa阅读(...)

评论(...)编辑

收藏

var cb_entryId= 12495157, cb_entryCreatedDate = '2020-03-14 23:23', cb_postType = 1;

loadViewCount(cb_entryId);

commentManager.renderComments(0);

googletag.cmd= googletag.cmd ||[];

googletag.defineSlot("/1090369/C1", [300, 250], "div-gpt-ad-1546353474406-0").addService(googletag.pubads());

googletag.defineSlot("/1090369/C2", [468, 60], "div-gpt-ad-1539008685004-0").addService(googletag.pubads());

googletag.pubads().enableSingleRequest();

googletag.enableServices();

});

if (new Date() >= new Date(2018, 9, 13)) {

googletag.cmd.push(function () { googletag.display("div-gpt-ad-1539008685004-0"); });

}

deliverBigBanner();

setTimeout(function() { incrementViewCount(cb_entryId); },50); deliverAdT2();

deliverAdC1();

deliverAdC2();

loadNewsAndKb();

loadBlogSignature();

LoadPostCategoriesTags(cb_blogId, cb_entryId); LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);

GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate, cb_postType);

loadOptUnderPost();

GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);

代码实现:#python实现杨辉三角形

defyanghui():#定义第一行列表为[1]

line = [1]whileTrue:#yield的作用:把一个函数变成生成器,同时返回一个list,下次从yield的下条语句执行

yieldline#设上一个为[1],通过式子可得[1,1],继而[1,2,1]......

line = [1] + [line[i] + line[i + 1] for i in range(len(line) - 1)] + [1]#输入杨辉三角形的行数

n = int(input("请输入行数:"))#定义一个结束的变量

flag =0#生成器可迭代,做个遍历

for i inyanghui():#打印每行的列表的元素,用空格连接

print(" ".join(str(j) for j ini))#打印完一行,flag+1

flag += 1

#如果变量flag等于输入的行数,跳出for循环

if flag ==n:#跳出循环

break效果:

请输入行数:611 11 2 11 3 3 11 4 6 4 11 5 10 10 5 1Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值