1 import time 2 from random import random 3 DARTS = 1000*1000 4 hits = 0.0 5 for i in range(1, DARTS+1): 6 x, y = random(), random() 7 dist = pow(x ** 2 + y ** 2, 0.5) 8 if dist <= 1.0: 9 hits = hits + 1 10 pi = 4 * (hits/DARTS) 11 scale = 50 12 print("执行开始".center(scale//2, "-")) 13 start = time.perf_counter() 14 for i in range(scale+1): 15 a = '*' * i 16 b = '.' * (scale - i) 17 c = (i/scale)*100 18 dur = time.perf_counter() - start 19 print(" 计算中:{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end='') 20 time.sleep(0.1) 21 print("圆周率值是: {}".format(pi)) 22 print(" "+"执行结束".center(scale//2,'-'))