说明
设置完毕后,即可开始倒计时,按Ctrl+C取消计时
[注意:请勿在编辑器中执行本程序]
代码
from time import sleep
set=''
hou=0
min=0
sec=0
while True:
#初始化
set=''
hou=0
min=0
sec=0
#设置
while True:
try:
set=input('请设置秒>')
sec=int(set)
except(ValueError):
print('\aError!')
pass
except(KeyboardInterrupt):
print('\r')
else:
break
while True:
try:
set=input('请设置分>')
if set=='':
min=0
else:
min=int(set)
except(ValueError):
print('\aError!')
pass
except(KeyboardInterrupt):
print('\r')
else:
break
while True:
try: #取消操作
set=input('请设置时>')
if set=='':
hou=0
else:
hou=int(set)
except(ValueError):
print('\aError!')
pass
except(KeyboardInterrupt):
print('\r')
else:
break
#转化
if sec>=60:
min=min+sec//60
sec=sec%60
if min>=60:
hou=hou+min//60
min=min%60
#开始
try:
input('Start...')
print('\n',end='')
sec=sec-1
while hou>=0:
while min>=0:
while sec>=0:
print('\r',hou,' : ',min,' : ',sec,'',end='')
sleep(1)
sec=sec-1
min=min-1
sec=59
hou=hou-1
min=59
print('\r时间到!',' '*5,'\n按 Ctrl+C 关闭\n')
#利用KeyboardInterrupt实现Ctrl+C 关闭
try:
while True:
print('\r\a',end='')
sleep(0.6)
except(KeyboardInterrupt):
pass
except(KeyboardInterrupt):
print('\r已取消',' '*(len(str(hou))+len(str(min))+len(str(sec))+6),'\n')
创作不易,点个赞再走呗