python怎么用圆周率_使用python计算圆周率(有进度条)

一、安装tqdm库

1、利用cmd命令提示符,进入python所在的文件盘

1631069-20190320220054530-730146766.png

2、输入 pip install tqdm 即可安装tqdm库

1631069-20190320220201413-700615131.png

二、输入代码(已经整合进度条与圆周率的代码)

from random importrandomfrom math importsqrtfrom time import *

from tqdm importtqdm

DARTS=10000000hits=0.0clock()for i in range(1,DARTS+1):

x,y=random(),random()

dist=sqrt(x**2+y**2)if dist <=1.0:

hits=hits+1pi=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()))

三、计算结果

1631069-20190320220510160-2131271957.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值