小型家庭项目

时间:2019-05-11 06:01:41

标签: python python-3.x

代码有点像这样:

import random

Employees = ["John", "tod", "mike", "Jim", "Kim", "liz", 
"lizzy", "jane", "nikki", "niko"]

Monday = []
Tuesday = []
Wednesday = []
Thursday =[]
Friday = []
Saturday = []
Sunday  = []
Off = []

Shift _one = random.choice(employees)
While Monday in range (3):
    Monday.append(shift_one)
Print(shift_one)

那是我的代码 我敢肯定,有一种更好的方法可以编写此代码。 我需要获取此代码以选择3个人进行3个班次,其余的要添加到关闭。 如果/ else语句,我认为将是必需的。我似乎无法正确地选择每天选择3个人(或总共3个人)的循环。

2 个答案:

答案 0 :(得分:0)

您大致处于正确的轨道。您应该研究的关键一件事是whilefor循环之间的区别以及它们的常规语法。另外,某些行的位置也已关闭。这需要实践,但是最终创建循环成为了第二天性。我在下面编写了一个示例供您查看(请注意,for循环中的idx变量是一个占位符变量):

for idx in range( 3 ):
    random_employee = random.choice( employees )
    Monday.append( random_employee )

print( Monday )

shift_one = Monday[ 0 ]
shift_two = Monday[ 1 ]
shift_three = Monday[ 2 ]

print( shift_one )
print( shift_two )
print( shift_three )

答案 1 :(得分:0)

已更新

在此答案中,每个工作日随机增加3名员工。我使用了一个频率计数器来选择3名要安排的员工。我添加了一些逻辑,以防止随机在同一天安排两次员工。我还添加了防止员工被使用超过4次的逻辑。

如果有人有改进此代码的建议,请告诉我。

employees = ["John", "Tod", "Mike", "Jim", "Kim", "Liz", "Lizzy", "Jane", "Nikki", "Niko"]

employees_per_day = 3

number_of_shifts = 22
max_number_of_shifts = 5

scheduled_work_days = []

# 7 day work week
monday = []
tuesday = []
wednesday = []
thursday = []
friday = []
saturday = []
sunday = []

def schedule_day_off():
  workweek = [monday, tuesday, wednesday, thursday, friday, saturday, sunday]
  flatten_workweek = [item for sublist in workweek for item in sublist]
  number_of_days = (Counter([employee for employee in flatten_workweek]))
  off_day = [name[0] for name in number_of_days.most_common(3)]
  return sorted(off_day)

def select_random_employee():
  random_employee = random.choice(employees)
  return random_employee

def max_work_days(employee_name):

  if len(scheduled_work_days) == 0 or len(scheduled_work_days) == number_of_shifts:
    return False

  elif len(scheduled_work_days) < number_of_shifts:
    total_days = {days: scheduled_work_days.count(days) for days in scheduled_work_days}
    for key, value in total_days.items():
       if employee_name not in key and value != max_number_of_shifts or value < max_number_of_shifts:
           return False
       elif employee_name in key and value == max_number_of_shifts:
           return True


for x in range(1, 22):
  scheduled_employee = select_random_employee()
  work_status = max_work_days(scheduled_employee)
  if work_status is False and len(monday) != 3 and scheduled_employee not in monday:
    scheduled_work_days.append(scheduled_employee)
    monday.append(scheduled_employee)
  elif work_status is False and len(tuesday) != 3 and scheduled_employee not in tuesday:
    scheduled_work_days.append(scheduled_employee)
    tuesday.append(scheduled_employee)
  elif work_status is False and len(wednesday) != 3 and scheduled_employee not in wednesday:
    scheduled_work_days.append(scheduled_employee)
    wednesday.append(scheduled_employee)
  elif work_status is False and len(thursday) != 3 and scheduled_employee not in thursday:
    scheduled_work_days.append(scheduled_employee)
    thursday.append(scheduled_employee)
  elif work_status is False and len(friday) != 3 and scheduled_employee not in friday:
    scheduled_work_days.append(scheduled_employee)
    friday.append(scheduled_employee)
  elif work_status is False and len(saturday) != 3 and scheduled_employee not in saturday:
    scheduled_work_days.append(scheduled_employee)
    saturday.append(scheduled_employee)
  elif work_status is False and len(sunday) != 3 and scheduled_employee not in sunday:
    scheduled_work_days.append(scheduled_employee)
    sunday.append(scheduled_employee)


print (f'Employees scheduled to work on Monday: {sorted(monday)}')
print (f'Employees scheduled to work on Tuesday: {sorted(tuesday)}')
print (f'Employees scheduled to work on Wednesday: {sorted(wednesday)}')
print (f'Employees scheduled to work on Thursday: {sorted(thursday)}')
print (f'Employees scheduled to work on Friday: {sorted(friday)}')
print (f'Employees scheduled to work on Saturday: {sorted(saturday)}')
print (f'Employees scheduled to work on Sunday: {sorted(sunday)}')

##############################
# Should this be an extra day? 
##############################
employees_schedule_off = schedule_day_off()
print (f'Employees scheduled off: {employees_schedule_off}')

# outputs 
Employees scheduled to work on Monday: ['Jim', 'Kim', 'Tod']
Employees scheduled to work on Tuesday: ['John', 'Liz', 'Lizzy']
Employees scheduled to work on Wednesday: ['Kim', 'Liz', 'Mike']
Employees scheduled to work on Thursday: ['Kim', 'Nikki', 'Tod']
Employees scheduled to work on Friday: ['John', 'Nikki', 'Tod']
Employees scheduled to work on Saturday: ['Jane', 'Niko', 'Tod']
Employees scheduled to work on Sunday: ['Kim', 'Lizzy', 'Niko']
Employees scheduled off: ['Kim', 'Liz', 'Tod']