我正在用Python为我的老师编写代码,我需要帮助使代码在指定的时间运行。现在,我只是想让代码运行,然后再将简单的功能实现到程序中...
我希望代码在每次上课时运行。我无法使它正常工作。下面是基本代码。
git cherry-pick <commit-1> <commit-2> <commit-3> ...
下面是我当前满意的代码-相反,我想用自动的类选择器替换下拉菜单。我建议运行此程序,以便您了解我的意思。
import datetime
from datetime import date
import calendar
while True:
my_date = date.today()
day=calendar.day_name[my_date.weekday()]
time_now = datetime.datetime.now()
if day == 'Monday':
if time_now.hour >= 9 and time_now.minute >= 10:
if time_now.hour >= 10 and time_now.minute >= 30:
if time_now.hour >= 11 and time_now.minute >= 30:
if time_now.hour >= 13 and time_now.minute >= 10:
if time_now.hour >= 14 and time_now.minute >= 5:
#period 5
else:
#period 4
else:
#period 3
else:
#period 2
else:
#period 1
else:
continue
#not in range
感谢您的帮助, 肖恩
答案 0 :(得分:0)
import datetime
import time
from datetime import date
import calendar
#definene your schedule in the dict
trigger_times = {
'Monday':'9:10,10:30,12:30',
'Tuesday':'9:10,10:30,12:30',
'Wednesday':'9:10,10:30,12:30',
'Thursday':'9:10,10:30,12:30',
'Friday':'9:10,10:30,12:30',
'Saturday':'9:10,10:30,12:30',
'Sunday':'9:10,10:30,12:30'}
triggered=False
while True:
my_date = date.today()
day=calendar.day_name[my_date.weekday()]
time_now = datetime.datetime.now()
task_for_today = trigger_times[day]#selects the schedule for today
#this returns list of the values with key current day
#you have trigger_items which is a dict
# day = *Monday*, then we pick the values from the key *Monday*
# hours to trigger = '9:10,10:30,12:30'
# next we split them and then we look if time_now is in there to execute the function
#these are the hours that the code is suppose to be executed
hours_to_trigger = task_for_today.split(',')
#time_now is the current time 10:30
time_now = str(time_now.strftime("%H:%M"))
#here checks if time_now in hours_to_trigger
# if '10:30' in ['9:10', '10:30', '12:30']
# if true will run the task
#triggered is the var that says if the task has been triggered you dont want it to loop for 60secs
if time_now in hours_to_trigger and triggered ==False:
print("Task is triggered in",time_now)
#here you put the trigger to the function you want to trigger
triggered == True
else:
triggered = False
答案 1 :(得分:0)
我已经用以下代码解决了问题:
import tkinter as tk # python 3
from tkinter import font as tkfont # python 3
from tkinter import StringVar
from tkinter import *
import datetime
from datetime import datetime, time
import calendar
import random
import datetime
from datetime import date
from datetime import *
from datetime import datetime
def getclass():
my_date = date.today()
day=calendar.day_name[my_date.weekday()]
now = datetime.now()
time_now = now.time()
def selecttimemonday():
if time(9,10) <= now.time() <= time(10,10):
print("Period 1 Monday")
elif time(10,30) <=now.time() < time(11,30):
print("Period 2 Monday")
year10cs()
elif time(11,30) <=now.time() < time(12,30):
print("Period 3 Monday")
elif time(13,10) <=now.time() < time(14,5):
print("Period 4 Monday")
elif time(14,5) <=now.time() < time(15,1):
print("Period 5 Monday")
else:
print("Not during class time")
def selecttimetuesday():
if time(8,35) <= now.time() <= time(9,40):
print("Period 1 Tuesday")
elif time(10,20) <=now.time() < time(11,20):
print("Period 2 Tuesday")
elif time(11,20) <=now.time() < time(12,20):
print("Period 3 Tuesday")
elif time(13,10) <=now.time() < time(14,5):
print("Period 4 Tuesday")
elif time(14,5) <=now.time() < time(15,1):
print("Period 5 Tuesday")
else:
print("Not during class time")
def selecttimewednesday():
if time(8,35) <= now.time() <= time(9,40):
print("Period 1 Wednesday")
elif time(10,20) <=now.time() < time(11,20):
print("Period 2 Wednesday")
year10cs()
elif time(11,20) <=now.time() < time(12,20):
print("Period 3 Wednesday")
elif time(13,10) <=now.time() < time(14,5):
print("Period 4 Wednesday")
elif time(14,5) <=now.time() < time(15,1):
print("Period 5 Wednesday")
else:
print("Not during class time")
def selecttimethursday():
if time(8,35) <= now.time() <= time(9,40):
print("Period 1 Thursday")
year10cs()
elif time(10,20) <=now.time() < time(11,20):
print("Period 2 Thursday")
elif time(11,20) <=now.time() < time(12,20):
print("Period 3 Thursday")
elif time(13,10) <=now.time() < time(14,5):
print("Period 4 Thursday")
elif time(14,5) <=now.time() < time(15,1):
print("Period 5 Thursday")
else:
print("Not during class time")
def selecttimefriday():
if time(8,35) <= now.time() <= time(9,40):
print("Period 1 Friday")
elif time(10,20) <=now.time() < time(11,20):
print("Period 2 Friday")
elif time(11,20) <=now.time() < time(12,20):
print("Period 3 Friday")
elif time(13,10) <=now.time() < time(14,5):
print("Period 4 Friday")
elif time(14,5) <=now.time() < time(15,1):
print("Period 5 Friday")
else:
print("Not during class time")
if day == "Monday":
selecttimemonday()
elif day == "Tuesday":
selecttimetuesday()
elif day == "Wednesday":
selecttimewednesday()
elif day == "Thursday":
selecttimethursday()
elif day == "Friday":
selecttimefriday()
else:
("Not weekday")
谢谢您的帮助!