导入time模块来计算代码运行的时间,加上代码迭代进度使用格式化字符串来输出代码运行进度
1 import time 2 scale = 50 3 print("执行开始,祈祷不报错".center(scale // 2,"-")) 4 start = time.perf_counter() 5 for i in range(scale + 1): 6 a = "*" * i 7 b = "." * (scale - i) 8 c = (i / scale) * 100 9 dur = time.perf_counter() - start 10 print(" {:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end = "") 11 time.sleep(0.1) 12 print(" "+"执行结束,万幸".center(scale // 2,"-"))