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

python基础-实现进度条功能,for和yield实现

2017-06-11 17:25 127 查看

实现进度条功能

 

方法一:简单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%")

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: