python进度条

本文介绍了一个使用Python编写的简单素数生成器程序,该程序能够生成指定范围内的所有素数,并通过一个进度条实时展示计算进度。文章包含了完整的代码实现。
import time
from math import sqrt
 
 
PW = 521025
 
def is_prime(n):
    for i in range(3, int(sqrt(n))+2, 2):
        if n % i == 0:
            return False
    return True
 
#进度条
class ProgressBar:
    def __init__(self, total=0, width=50):
        self.total = total
        self.width = width
 
    def show(self, count, done='#', wait='-'):
        proc = self.width * count // self.total
        ok, undo = done * proc, wait * (self.width-proc)
        #print(f'\rRunning... [{ok}{undo}] {count}/{self.total}'.format(ok,undo,count,self.total), end='')
        print("\rRunning... [{0}{1}] {2}/{3}".format(ok,undo,count,self.total), end='')
 
 
def main(total=PW):
    n = 3
    bar = ProgressBar(total)
    for p in range(2, total):
        while True:
            n += 2
            if is_prime(n):
                bar.show(p+1)
                break
 
 
if __name__ == '__main__':
    main()

转载于:https://www.cnblogs.com/yushengzhou/p/9638580.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值