'Is there a way to to make a random module not to use a specific key in Python?

I'm pretty new to python and at the moment I'm working on a work shift generator. I made it quite easy, as it just randomly sets shifts for the whole week. But I want it to work the following way:

  • each employee must have two days off
  • if the first employee has a day off, the second employee should have a work shift that day At the moment, I came up with the following code:
import random

# Shift types
shifts = ["9:00 - 17:00", "16:00 - 00:00"]

# Schedule for employees
schedule = {1: '16:00 - 00:00', 2: 'X', 3: 'X', 4: '16:00 - 00:00', 5: '16:00 - 00:00', 6: '16:00 - 00:00', 7: '16:00 - 00:00'}
schedule_dg = {1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: []}

def get_1_week_dg(schedule, schedule_dg, shift):
    for i in schedule:
        if schedule[i] == "X":
           schedule_dg[i] = random.choice(shift)
    for d in random.sample(range(1, 7 + 1), k=2):
        if schedule_dg[d] == []:
            schedule_dg[d] = "X"
    return schedule_dg

So the issue is that the code still places a day off on a day, that must be a working day. I'd appreciate any help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source