圆周率进度条
import time
from random import random
scale = 10
print("执行开始".center(scale//2, "-"))
start=time.perf_counter()
DARTS=1000*1000
hits=0.0
for i in range(1,DARTS):
x,y=random(),random()
dist=pow(x**2+y**2,0.5)
if dist<=1.0:
hits=hits+1
pi=4*(hits/DARTS)
for i in range(scale+1):
a = '*' * i
b = '.' * (scale - i)
c = (i/scale)*100
dur = time.perf_counter() - start
print("{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur))
time.sleep(0.1)
print("圆周率值是:{}".format(pi))
print("\n"+"执行结束".center(scale//2,'-'))
import time
from random import random
scale = 10
print("执行开始".center(scale//2, "-"))
start=time.perf_counter()
DARTS=1000*1000
hits=0.0
for i in range(1,DARTS):
x,y=random(),random()
dist=pow(x**2+y**2,0.5)
if dist<=1.0:
hits=hits+1
pi=4*(hits/DARTS)
for i in range(scale+1):
a = '*' * i
b = '.' * (scale - i)
c = (i/scale)*100
dur = time.perf_counter() - start
print("{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur))
time.sleep(0.1)
print("圆周率值是:{}".format(pi))
print("\n"+"执行结束".center(scale//2,'-'))

浙公网安备 33010602011771号