p y t h o n python python — 模拟进度条
之前我们讲到了python的进度条,唯一美中不足的是它会换行。
没看过的小伙伴们点击这个链接:
print模拟加载进度条
测试
请大家做个选择题:
print("helloworld")
程序会输出啥?
A:hello
B:world
C:helloworld
D:hello
world
答案:C
那这个呢?
print("hello\rworld")
A:hello
B:world
C:helloworld
D:hello
world
答案:B
\r
\r是一个转义字符,让他回到一行开始(即hello)的地方继续打印(即world)
升级后
import time
import random
for i in range(100):
print("\r",i,'%',end = "",flush = True) #会快一点
time.sleep(random.randint(1,15) / 100) #延迟时间你自己定
time.sleep(random.randint(10,40) / 100) #延迟时间你自己定
print("\r",'100 %')
小结
今天我们知道了print模拟进度条,还知道了转义字符\r。