rt. 适用于浙江考生。
[Python] 纯文本查看 复制代码 from time import time, sleep
from os import system
first = 1609891200 # 2020/1/6 8:00
second = 1623024000 # 2021/6/7 8:00
now = int(time())
width = 22
if now < first: target = first; tip = '首考倒计时'.center(width-5)
elif now < second: target = second; tip = '高考倒计时'.center(width-5)
else: print('Good luck!'); tip = ''
system(f'mode con: cols={width} lines=2')
sleep(0.1)
print(tip)
t = target - now
def f(x):
return str(x) if x >= 10 else f'0{x}'
while t >= 0:
t = target - int(time())
d = t // 86400
t = t % 86400
h = f(t // 3600)
m = f(t // 60 % 60)
s = f(t % 60)
print(f'{d}天{h}时{m}分{s}秒'.center(width-4), end='\r')
sleep(1)
input()
运行界面如下:
win10效果
如果前面XX倒计时的字被很多行时间覆盖,使用下面的代码:
[Python] 纯文本查看 复制代码 from time import time, sleep
from os import system
first = 1609891200 # 2020/1/6 8:00
second = 1623024000 # 2021/6/7 8:00
now = int(time())
width = 22
if now < first: target = first; tip = '首考倒计时'.center(width-5)
elif now < second: target = second; tip = '高考倒计时'.center(width-5)
else: print('Good luck!'); tip = ''
system(f'mode con: cols={width} lines=3')
sleep(0.1)
print(tip)
t = target - now
def f(x):
return str(x) if x >= 10 else f'0{x}'
while t >= 0:
t = target - int(time())
d = t // 86400
t = t % 86400
h = f(t // 3600)
m = f(t // 60 % 60)
s = f(t % 60)
print(f'{tip}\n'+f'{d}天{h}时{m}分{s}秒'.center(width-4))
sleep(1)
system('cls')
input()
linux下把倒数第二行的system('cls')改为system('clear')即可使用。 |