目錄python
用過計算機的都見過算法
如何得到文本進度條的變化時間?函數
# TextProBarV1.py import time scale = 10 print("------執行開始------") for i in range(scale + 1): a = '*' * i b = '.' * (scale - i) c = (i / scale) * 100 print("{:^3.0f}%[{}->{}]".format(c, a, b)) time.sleep(0.1) print("------執行結束------")
------執行開始------ 0 %[->..........] 10 %[*->.........] 20 %[**->........] 30 %[***->.......] 40 %[****->......] 50 %[*****->.....] 60 %[******->....] 70 %[*******->...] 80 %[********->..] 90 %[*********->.] 100%[**********->] ------執行結束------
刷新的關鍵是 \r
spa
print()
須要被控制\r
注意:IDLE如Pycharm屏蔽了\r
功能設計
# TextProBarV2.py import time for i in range(101): print("\r{:3}%".format(i), end="") time.sleep(0.1)
100%
# TextProBarV3.py import time scale = 10 print("執行開始".center(scale // 2, "-")) 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) print("\n" + "執行結束".center(scale // 2, '-'))
-執行開始 100%[**********->]1.03s -執行結束
計算問題擴展code
perf_counter()
計時進度條應用orm
文本進度條的不一樣設計函數blog
設計名稱 | 趨勢 | 設計函數 |
---|---|---|
Linear | Constant | \(f(x) = x\) |
Early Pause | Speeds up | \(f(x) = x+(1-sin(x*π*2+π/2)/-8\) |
Late Pause | Slows down | \(f(x) = x+(1-sin(x*π*2+π/2)/8\) |
Slow Wavy | Constant | \(f(x) = x+(1-sin(x*π*2+π/2)/8\) |
Fast Wavy | Constant | \(f(x) = x+(1-sin(x*π*2+π/2)/8\) |
Power | Speeds up | \(f(x) = {(x+(1-x)*0.03)}^2\) |
Inverse Power | Slows down | \(f(x) =1+{(1-x)}^{1.5}*-1\) |
Fast Power | Speeds up | \(f(x) = {(x+(1-x)/2)}^8\) |
Inverse Fast Power | Slows down | \(f(x) = 1+{(1-x)}^3*-1\) |