有进度条的圆周率

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------")

猜你喜欢

转载自www.cnblogs.com/ssh-4016/p/12601089.html