Python计算pi及其进度条显示

上周老师布置了一个课后作业,去尽可能的准确计算π的值,还要显示时间和进度条,对于python小白的我,当然是综合书上和网上的知识,自己做了一个小程序,代码如下:
一、写代码的准备工作:用pip下载第三方库tqdm
步骤1:打开cmd
步骤2:输入pip install 你要安装的库(如 pip install tqdm) #pip一般是在安装python的时候就有了,但是我还没有tqdm库,所以就要去下载

 

二、写程序

from math import *
from tqdm import tqdm
from time import *
total,s,n,t=0.0,1,1.0,1.0
clock()
while(fabs(t)>=1e-6):
total+=t
n+=2
s=-s
t=s/n
k=total*4
print("π值是{:.10f} 运行时间为{:.4f}秒".format(k,clock()))
for i in tqdm(range(101)):
print("\r{:3}%".format(i),end="")
sleep((clock())/100)#用执行程序的总时间来算出进度条间隔的时间

  三、效果图如下:

 

 

posted @ 2019-03-20 23:42  傲博不太傲  阅读(598)  评论(0编辑  收藏  举报