【发布时间】:2020-12-27 04:24:11
【问题描述】:
我正在按照 Youtube 视频的步骤进行倒计时,但有一件事我不明白,那就是在 print (time_left + '\r', end = '') 行中使用回车 \r 如何与 end = '' 一起工作能够擦除同一行并再次写入吗?因为如果我只写\r,数字就会出现在新行中。
while True:
uin = input('')
try:
when_to_stop = abs(int(uin))
except KeyboardInterrupt:
break
except:
print('That\'s not a number')
while when_to_stop > 0:
m, s = divmod(when_to_stop, 60)
h, m = divmod(m, 60)
when_to_stop = when_to_stop - 1
time_left = str(h).zfill(2)+ ':' + str(m).zfill(2) + ':' + str(s).zfill(2)
print(time_left + '\r', end = '')
time.sleep(1)
【问题讨论】:
-
@L3viathan:这是一个完整的答案,不妨把它变成一个。
标签: python-3.x time