有进度条的圆周率
Posted ssh-4016
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有进度条的圆周率相关的知识,希望对你有一定的参考价值。
from random import random import time import math print("------Begin------") start = time.perf_counter() scale = 10 for i in range(scale+1): a = ‘*‘*i b = ‘.‘*(scale-i) c = (i/scale)*100 print("{:^3.0f}%[{}->{}]".format(c,a,b)) time.sleep(0.5) DARTS = 1000*1000 hits = 0.0 start = time.perf_counter() for i in range(1,DARTS+1): x,y = random(),random() dist = pow(x**2+y**2,0.5) if dist<=1.0: hits += 1 pi = 4*(hits/DARTS) print("圆周率为:{}".format(pi)) end=time.perf_counter() print("运行时间:{:.2f}s".format(end-start)) print("------End------")
以上是关于有进度条的圆周率的主要内容,如果未能解决你的问题,请参考以下文章