首页 > 编程语言 > 详细

python基础学习日志day8-实现进度条功能,for和yield实现

时间:2017-06-11 18:04:50      阅读:331      评论:0      收藏:0      [点我收藏+]

实现进度条功能

 

方法一:简单FOR实现打印进度条功能
for i in range(10):
    print("#",end="",flush=True)
    time.sleep(0.4)

 

#方法二,yeild实现复杂进度条功能

def show_process(total):
    recive_size=0
    current_size=0

    while recive_size<total:

        if int(recive_size/total*100) >current_size: #进度比现在的大
            print("#",end="",flush=True)

            current_size=int(recive_size/total*100)
        new_size=yield  #中断
        recive_size+=new_size

total=10000000
recive_size=0

precess=show_process(total) #调用getnerator
precess.__next__()
while recive_size <total:
    recive_size+=10
    try:
        precess.send(10) #给generotor发送数据,并继续yield后面的执行
    except StopIteration as e:
        print("100%")

 

python基础学习日志day8-实现进度条功能,for和yield实现

原文:http://www.cnblogs.com/lixiang1013/p/6985712.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!