用python算圆周率及进度条提示

(一)圆周率 :

           (1)圆周率是指平面上圆的周长与直径之比 (ratio of the circumference of a circle to the diameter) 。用符号π表示。中国古代有圆率、圆率、周等名称。

           (2)计算公式:π/4=1-1/3+1/5-1/7+.......

(二)代码如下:

import math
import time
scale=14 #scale的值越大,π的值越精确,但运算时间会加长
s,m,=1,2
total,s,n,t=0.0,1,1.0,1.0
print("执行开始".center(scale//2, "-"))
start = time.perf_counter()
for i in range(scale+1):
    total+=t
    n+=2
    s=-s
    t=s/n
    k=total*4
    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))
time.sleep(0.1)
Pi=k
print("Pi值是{}".format(Pi))
print("\n"+"执行结束".center(scale//2,'-'))

(三) 执行结果如下: 

 

 

 

posted @ 2019-03-19 19:28    阅读(540)  评论(0编辑  收藏  举报