计算Π
一、写代码的准备工作:用pip下载第三方库tqdm
1.打开cmd
2.输入pip install 你要安装的库(如 pip install tqdm) #pip一般是在安装python的时候就有了,还有tqdm我之前已经下载好了
二、写程序
1.记忆中的C中的一道例题,正好提供一个算法
2.输入以下代码
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(100)): print("\r{:3}%".format(i),end="") sleep((clock())/100)
三、运行程序
1.过程一
2.运行最终结果如下