经过一定时间后,如何重新运行if语句?

时间:2019-07-14 16:45:24

标签: python

我正在尝试编写一个程序,该程序在小时改变后按时间表打印当前任务。例如,在9点钟,它将显示“完成任务1的时间”。 10点“执行任务2的时间”,依此类推。使用while语句检查要更改的小时数会导致计算机占用过多的内存,因此我想到只检查每分钟是几小时,然后检查一下时间间隔(例如,当它是9点钟时)然后执行print语句),它将停止检查时间,直到小时结束为止(分钟数现在存储在名为“ a”的变量中)。如何在每个时间间隔执行if语句?

如前所述,我试图用一会儿语句解决问题,但笔记本电脑的电池耗费了很多钱。 while语句也不适用于变量“ a”。

from datetime import date
from datetime import time
from datetime import datetime

#Functions
def getWeekday(num):
    if num == 0:
        return 'Monday'
    elif num == 1:
        return 'Tuesday'
    elif num == 2:
        return 'Wednesday'
    elif num == 3:
        return 'Thursday'
    elif num == 4:
        return 'Friday'
    elif num == 5:
        return 'Saturday'
    else:
        return 'Sunday'

def scheduleRun(day):
    print('Today is {0}'.format(day))
    if day == 'Sunday':
        #Check what hour it is every minute to see if it changes
        if now.strftime('%H') == 9:
            print('Time for Task 1')
            a = 60 - int(now.strftime('%M'))
             #When it is confirmed that it is 9 o'clock,
             #Find how many minutes until the hour changes, then run the series of if statements again
        elif now.strftime('%H') == 10:
            print('Time for Task 2')
        elif now.strftime('%H') == 11:
            print('Lunchtime!')
        elif now.strftime('%H') == 12:
            print('Time for task 3')
    if day == 'Monday':
        pass
    if day == 'Tuesday':
        pass
    if day == 'Wednesday':
        pass
    if day == 'Thursday':
        pass
    if day == 'Friday':
        pass
    if day == 'Saturday':
        pass

#Get Today's Date
today = date.today()
now = now.datetime()
scheduleRun(getWeekday(today.weekday()))

0 个答案:

没有答案