用python进行有进度条的圆周率计算

一、安装tqdm函数库

tqdm是一个强大的终端进度条工具,我利用pip获取tqdm函数库。

1、打开运行,输入“cmd”

2、2:输入pip install   你要安装的库(如 pip install tqdm)

此处我已经装好了。

 二、编写代码

我用书上的代码

from random import random
from math import sqrt
from time import *
from tqdm import tqdm
DARTS=10000000
hits=0.0
clock()
for i in range(1,DARTS+1):
    x,y=random(),random()
    dist=sqrt(x**2+y**2)
    if dist <=1.0:
        hits=hits+1
pi=4*(hits/DARTS)
for i in tqdm(range(10)):
    print("\r{:3}%".format(i/10*100),end="") #这里的i/10*100指每10%显示一次
sleep((clock())/100)#用执行程序的总时间来算出进度条间隔的时间
print("pi的值{}.".format(pi))
print("运行时间:{:.5f}s".format(clock()))
三、执行效果

 

 
   
posted on 2019-03-20 11:02  loverboy88 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/loverboy88/p/10563607.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值