Python倒计时时钟与岁月

时间:2017-10-16 07:23:33

标签: python

目前致力于制作倒计时时钟,该时钟采用未来日期并持续返回该日期之前的时间量。但是,我似乎无法弄清楚如何创建一个条件,一旦定时器降到0以下,循环就会关闭。我的当前代码:

from datetime import datetime
import time

def clock():
    print ('Please enter a date and time in the format dd,mm,yyyy,hour,minute,second')
    response = input() #intake date and time
    respolist = response.split(',') #split into 5 individual items
    while True:
        try:
            print (datetime(int(respolist[2]), int(respolist[1]), int(respolist[0]), int(respolist[3]), int(respolist[4]), int(respolist[5])) - datetime.now())
            time.sleep(1)
            if #timer runs below 0:
                break
        except ValueError:
            print('Something was wrong with the date entered, please try again.')
            clock()
clock()

非常感谢可以提供的任何帮助

1 个答案:

答案 0 :(得分:0)

您可以使用剩余时间设置变量,并在秒小于0时检查它:

    try:
        remaining_time=datetime(int(respolist[2]), int(respolist[1]), int(respolist[0]), int(respolist[3]), int(respolist[4]), int(respolist[5])) - datetime.now()
        print (remaining_time)
        time.sleep(1)
        if remaining_time.seconds <= 0:
            print 'CONGRATS !'
            break