第一种进度条
import sys
import time
def progress_bar():
for i in range(1, 101):
print("\r", end="")
print("Download progress: {}%: ".format(i), "▋" * (i // 2), end="")
sys.stdout.flush()
time.sleep(0.05)
if __name__ == '__main__':
progress_bar()
PI = 0
N = 1000
for n in range(int(N)):
PI += 1/pow(16,n) * (4/(8*n+1) - 2/(8*n+4) - 1/(8*n+5) - 1/(8*n+6))
print("\n")
print(PI)
执行结果如下:
第二种不进行动态刷新的进度条
import sys
import time
scale=10
print("-----执行开始------")
for i in range(scale+1):
a,b='**'*i,'..'*(scale-i)
c=(i/scale)*100
print("%{:^3.0f}[{}]".format(c,a,b))
time.sleep(0.1)
print("------执行结束-------")
PI = 0
N = 1000
for n in range(int(N)):
PI += 1/pow(16,n) * (4/(8*n+1) - 2/(8*n+4) - 1/(8*n+5) - 1/(8*n+6))
print("\n")
print(PI)
执行结果如下:
第三种进行单行动态刷新的进度条
import sys
import time
import time
for i in range(101):
print("\r{:3}%".format(i),end="")
time.sleep(0.05)
PI = 0
N = 1000
for n in range(int(N)):
PI += 1/pow(16,n) * (4/(8*n+1) - 2/(8*n+4) - 1/(8*n+5) - 1/(8*n+6))
print("\n")
print(PI)
执行结果如下:
第四种带刷新文本的进度条
import time
scale=50
print("执行开始".center(scale//2,'-'))
t=time.perf_counter()
for i in range(scale+1):
a='*'*i
b='.'*(scale-i)
c=(i/scale)*100
t-=time.perf_counter()
print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,c,-t),end='')
time.sleep(0.05)
print("\n"+"执行结束".center(scale//2,'-'))
PI = 0
N = 1000
for n in range(int(N)):
PI += 1/pow(16,n) * (4/(8*n+1) - 2/(8*n+4) - 1/(8*n+5) - 1/(8*n+6))
print("\n")
print(PI)
执行结果如下: