Python计算休假日从一个轮班工施

2024-04-25 00:50:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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])

Tags: theindatetimedatesatworkingsunweek
2条回答

这将生成一个每周时间表的列表。每周一到周日的每周时间表都是一个列表。星期一到星期天的选择只是为了与Python在datetimecalendar模块中的星期几的顺序一致。在

weeks = []
num_weeks = 4 # how many weeks to schedule
days_off = [
    (2, 6),   # sunday and wednesday
    (0, 5),   # monday and saturday
]

for week in range(num_weeks):
    days_off_this_week = days_off[week % len(days_off)]
    weeks.append([bool(i not in days_off_this_week) for i in range(7)])

这里的关键是使用%运算符(week % len(days_off)),而不是//来确定我们应该使用哪一周的日程安排。因为我们使用len(days_off),所以只要在days_off中添加一个条目,我们就可以让我们的2周轮换变成3周或4周的轮换,日历仍然可以工作。在

将星期几存储为整数,将开/关存储为布尔值,为以后如何组织显示这些信息提供了很大的灵活性。我们可以使用日历模块轻松打印时间表:

^{pr2}$

要更改显示中每周的第一个工作日,我们只需将一周中的另一天传递给calendar.Calendar(例如calendar.Calendar(calendar.MONDAY))。在

这并没有考虑到实际的日期,但如果有用的话,我很乐意补充一下。在

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:
    if weekday == 1:
        print ("You are: %s" % (sched2['Mon']))
    elif weekday == 2:
        print ("You are: %s" % (sched2['Tues']))
    elif weekday == 3:
        print ("You are: %s" % (sched2['Wed']))
    elif weekday == 4:
        print ("You are: %s" %  (sched2['Thur']))
    elif weekday == 5:
        print ("You are: %s" %  (sched2['Fri']))
    elif weekday == 6:
        print ("You are: %s" %  (sched2['Sat']))
    elif weekday == 7:
        print ("You are: %s" %  (sched2['Sun']))
elif weekno % 2 != 0:
    if weekday == 1:
        print ("You are: %s" %  (sched1['Mon']))
    elif weekday == 2:
        print ("You are: %s" %  (sched1['Tues']))
    elif weekday == 3:
        print ("You are: %s" %  (sched1['Wed']))
    elif weekday == 4:
        print ("You are: %s" %  (sched1['Thur']))
    elif weekday == 5:
        print ("You are: %s" %  (sched1['Fri']))
    elif weekday == 6:
        print ("You are: %s" %  (sched1['Sat']))
    elif weekday == 7:
        print ("You are: %s" %  (sched1['Sun']))

相关问题 更多 >