What I am Trying To Do:
We have a date range. Once the range is created then I am trying to "assign" a person for a full week (by adding them to the list of the specific day). Then once that week is completed then move to the next person in the onCall list and assign them a full week of on call until we have covered all the weeks.
The Problem:
It assigns out the first week to the first person but stops assigning to the next person in the list. So in this instance Billy gets the first week assigned to him, but then the other 3 weeks are empty when they should have Todd, Moose, and DJ assigned.
I believe the problem is with the nested while loop. From what I can tell it is breaking out of the while loop, but for some weird reason that stops it from being able to go to the next person. Any help would be awesome as this is driving me crazy.
import datetime
start_date = datetime.date(2021, 11, 1)
end_date = datetime.date(2021, 11, 28)
delta = datetime.timedelta(days=1)
Days = []
while start_date <= end_date:
taskday = str(start_date)
taskdaylist = taskday = [taskday]
Days.append(taskday)
start_date = delta
daysLength = len(Days)
daysi = 0
onCall = ["Billy", "Todd", "Moose", "DJ", "Kach", "Darwin", "Freddy", "Todd"]
personcounter = 0
lengthOfonCall = len(onCall)
weekdaycounter = 0
r = 0
weeks = daysLength/7
while r < weeks:
personOnCall = "OnCall: " onCall[personcounter]
while weekdaycounter < 7:
r = 1 r
Days[daysi].append(personOnCall)
weekdaycounter = weekdaycounter 1
daysi = 1 daysi
personcounter = 1 personcounter
weekdaycounter = 0
if (personcounter == lengthOfonCall):
personcounter = 0
print(Days)
CodePudding user response:
I am not 100% sure what you want to achieve. But I think problem is that you increment variable r inside nested while-loop. You should increment it after nested while-loop.
Second advice use r = 1
instead of r = 1 r
.
Here is the about from your code after suggested change.
[['2021-11-01', 'OnCall: Billy'], ['2021-11-02', 'OnCall: Billy'], ['2021-11-03', 'OnCall: Billy'], ['2021-11-04', 'OnCall: Billy'], ['2021-11-05', 'OnCall: Billy'], ['2021-11-06', 'OnCall: Billy'], ['2021-11-07', 'OnCall: Billy'], ['2021-11-08', 'OnCall: Todd'], ['2021-11-09', 'OnCall: Todd'], ['2021-11-10', 'OnCall: Todd'], ['2021-11-11', 'OnCall: Todd'], ['2021-11-12', 'OnCall: Todd'], ['2021-11-13', 'OnCall: Todd'], ['2021-11-14', 'OnCall: Todd'], ['2021-11-15', 'OnCall: Moose'], ['2021-11-16', 'OnCall: Moose'], ['2021-11-17', 'OnCall: Moose'], ['2021-11-18', 'OnCall: Moose'], ['2021-11-19', 'OnCall: Moose'], ['2021-11-20', 'OnCall: Moose'], ['2021-11-21', 'OnCall: Moose'], ['2021-11-22', 'OnCall: DJ'], ['2021-11-23', 'OnCall: DJ'], ['2021-11-24', 'OnCall: DJ'], ['2021-11-25', 'OnCall: DJ'], ['2021-11-26', 'OnCall: DJ'], ['2021-11-27', 'OnCall: DJ'], ['2021-11-28', 'OnCall: DJ']]
CodePudding user response:
Unless I misunderstood the question, it's as simple as this:
import datetime
start_date = datetime.date(2021, 11, 1)
end_date = datetime.date(2021, 11, 28)
onCall = ["Billy", "Todd", "Moose", "DJ", "Kach", "Darwin", "Freddy", "Todd"]
delta = datetime.timedelta(days=7)
for oc in onCall:
print(f'{oc} is on call for week commencing {start_date}')
if (start_date := start_date delta) > end_date:
break