I'm new to python, and I can't figure out any strategy or existing module that would solve this problem. I would like to make a simple program that would return if a given day in the future would be a day off or not based on the following schedule (actual schedule for this week of July 2018):
Week 1: 1:Sun:off 2:Mon 3:Tues 4:Wed:off 5:Thur 6:Fri 7:Sat Week 2: 8:Sun: 9:Mon:off 10:Tues 11:Wed 12:Thur 13:Fri 14:Sat:off Week 3:15:Sun:off 16:Mon 17:Tues 18:Wed:off 19:Thur 20:Fri 21:Sat Week 4:22:Sun 23:Mon:off 24:Tues 25:Wed 26:Thur 27:Fri 28:Sat:off
import datetime
sched1 = {'Mon':'working','Tues':'working','Wed':'off','Thur':'working','Fri':'working','Sat':'working','Sun':'off'}
sched2 = {'Mon':'off','Tues':'working','Wed':'working','Thur':'working','Fri':'working','Sat':'off','Sun':'working'}
pickdate1 = int(input("Enter a date in the year (YYYY)): "))
pickdate2 = int(input("Enter a date in the year (MM): "))
pickdate3 = int(input("Enter a date in the year (DD): "))
date = datetime.date(pickdate1,pickdate2,pickdate3)
weekno = datetime.date(pickdate1,pickdate2,pickdate3).isocalendar()[1]
weekday = datetime.date.isoweekday(date)
if weekno % 2 == 0:
print (sched2[weekday])
elif weekno % 2 != 0:
print (sched1[weekday])
这将生成一个每周时间表的列表。每周一到周日的每周时间表都是一个列表。星期一到星期天的选择只是为了与Python在datetime和calendar模块中的星期几的顺序一致。在
这里的关键是使用
%
运算符(week % len(days_off)
),而不是//
来确定我们应该使用哪一周的日程安排。因为我们使用len(days_off)
,所以只要在days_off
中添加一个条目,我们就可以让我们的2周轮换变成3周或4周的轮换,日历仍然可以工作。在将星期几存储为整数,将开/关存储为布尔值,为以后如何组织显示这些信息提供了很大的灵活性。我们可以使用日历模块轻松打印时间表:
^{pr2}$要更改显示中每周的第一个工作日,我们只需将一周中的另一天传递给
calendar.Calendar
(例如calendar.Calendar(calendar.MONDAY)
)。在这并没有考虑到实际的日期,但如果有用的话,我很乐意补充一下。在
相关问题 更多 >
编程相关推荐