我正在编写简单的倒计时代码,但无法覆盖时间。 结果以一行形式出现(如 00:00:0200:00:01),我希望将其堆叠起来,看起来像一个实际的时钟
print(time_left,end="")
这是我所做的,我将 \r
设为
print("time_left + "\r", end="")
没有成功
import time
while True:
uin = input(">> ")
try:
when_to_stop = abs(int(uin))
except KeyboardInterrupt:
break
except:
print("Not a number!")
while when_to_stop > 0:
m, s = divmod(when_to_stop, 60)
h, m = divmod(m, 60)
#.zfill(2) to make 00:00:00 form
time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
print(time_left + '\r', + end="")
time.sleep(1)
when_to_stop -= 1
print()
最佳答案
您需要使用
print(time_left,end='\r')
end='\r'
将光标返回到行的开头。因此,当您再次打印相同的内容时,它会被覆盖。
关于python - while 循环覆盖结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57471971/