python计算圆周率及其进度条

pi = 0
N = 100
import sys
import time
for k in range(N):
pi += 1/pow(16, k) * (4 / (8 * k + 1) - 2 /(8 * k + 4) - 1/(8 * k + 5) - 1 /(8 * k + 6))
print("圆周率值是:{}".format(pi))

print("\r",end='')
print("progress:{}%:".format(k+1),"#"*(k//2),end="\n\n")
sys.stdout.flush()
time.sleep(0.01)

 

 加上时间的进度条:

import time
scale=50
start=time.perf_counter()
for i in range(scale+1):
a='*'*i
b="."*(scale-i)
c=(i/scale)*100
dur=time.perf_counter()-start
print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end='')
time.sleep(0.1)

 

posted @ 2021-10-16 12:22  周楚y  阅读(146)  评论(0)    收藏  举报