Forum Archive

Schedule

omzo

I am learning python:
I am learning python. I need help to code that print the current shift any given time.

Shift1Week1: Sunday: Work, Monday: Work:Tuesday: Work, Wednesday: Work, Thursday: Off, Friday: Off, Saturday: Off
Shift1Week2: Sunday:Work, Monday: Work, Tuesday: Work, Wednesday: Off, Thursday: Off, Friday: Off, Saturday: Off
The above schedule keeps alternating (infinite loop)

Shift1 and Shift3 work every other Wednesday
When Shift1 is off, Shift3 works and vise versa
These are 12hrs shift (6AM to 6PM)

Shift2Week1: Saturday: Off, Sunday:Work, Monday:Work, Tuesday:Work, Wednesday:Off, Thursday:Off, Friday:Off
Shift2Week2: Saturday:Work, Sunday:Work, Monday:Work, Tuesday:Work, Wednesday:Off, Thursday:Off, Friday:Off
The above schedule keeps alternating (infinite loop)

Shift2 and Shift4 work every other Saturday.
If Shift2 is off, Shift4 works
Shift2 and Shift4 works 12hrs shifts (6PM to 6AM )

JonB

So, ignoring python, how would you as a person figure this out, way with pencil and paper?

Often that is the first way to approach a problem -- just talking it out.

Next, you would probably want to think of what your inputs and outputs should be. For instance, I think you want a function that, Given a date, tells you which shifts are working?

So you'd write that in python as

def working_shifts ( date ):
    ''' step 1: figure out what week and day of week it is from the date.'''
   ''' step 2: figure out which shift is working '''
    return shifts

For step 1, read the docs about datetime.date. that has some methods for getting weekday and week.

For step 2, you'll either need to figure out an algorithm that describes the shift schedule, or hard code it in a dictionary or list of lists.

Give it a go, and post back what you've got.

ccc

In addition to the great advice from @JonB, simplify your mental model and your data by focusing on knowing the days_off. Python makes it simple to calculate the days_on if you know the days_off.

>>> import calendar
>>> days_off = ("Saturday", "Sunday")
>>> days_on = [day for day in calendar.day_name if day not in days_off]
>>> days_on
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']