用python的time库写一个进度条

时间:2022-09-16 12:43:13

运算符

算数运算

如a=10,b=20

+两个数相加 a+b=30

-两个数相减 a-b=-10

*两个数相乘 a*****b =200

/两个数相除b/a=2

%取模,并返回余数b%a=0

幂,a**b=10的20次方

.strip()去除多余空格字符串

.isdigit()判断是否为数字字符串

time库

  • 获取时间

    time.timr()

    time.ctime()

    time.gmtime()

  • 格式化

​ time.strftime

​ time.strptime

  • 程序计时

    time.sleep()

    time.perf_counter()

利用time库写一个进度条

  • 源代码

    import time
    
    count = 10
    print("....请开始执行....")
    for i in range(count + 1):
    a = "#" * i
    b = "." * (count - i)
    c = (i/count) * 100
    print(f"[{a}->{b}]{c}%")
    time.sleep(0.2)
    print("......执行结束.....")

优化后的进度条

  • 源代码

    import time
    
    print("开始执行".center(50, '-'))#把开始执行放中间,用50个-包围
    start = time.perf_counter()
    count = 100
    for i in range(count + 1):
    a = "#" * i
    b = "." * (count - i)
    c = (i/count) * 100
    res = time.perf_counter() - start
    #print(f"\r[{a}->{b}]{c:.2f}%{res:.2f}s",end="")
    print(f"\033[1;31m\r[{a}->{b}]{c:.2f}%{res:.2f}s\033[0m", end="")
    # 上面加了就可以改变颜色
    #print(f"\033[1;31;40m\r[{a}->{b}]{c:.2f}%{res:.2f}s\033[0m", end="")
    #上面这行还可以改变进度条背景颜色
    time.sleep(0.05)
    print("\n" + "结束执行".center(50, '-')) ```![](https://img2018.cnblogs.com/blog/1843751/201910/1843751-20191023191027296-773847820.png)