小学课程自动排课 Python 代码示例
以下是一个简单的小学课程排课的 Python 代码示例:
# 定义小学课程
courses = ['语文', '数学', '英语', '科学', '体育', '音乐', '美术']
# 定义上课时间段
time_slots = ['周一上午', '周一下午', '周二上午', '周二下午', '周三上午', '周三下午', '周四上午', '周四下午', '周五上午', '周五下午']
# 定义排课结果
schedule = {}
# 遍历所有课程
for course in courses:
# 随机选择一个时间段
time_slot = random.choice(time_slots)
# 将课程添加到排课结果中
schedule[course] = time_slot
# 从时间段列表中移除已排课的时间段
time_slots.remove(time_slot)
# 打印排课结果
for course, time_slot in schedule.items():
print(f'{course}: {time_slot}')
这段代码使用了一个列表 courses 存储所有的小学课程,一个列表 time_slots 存储上课时间段,一个字典 schedule 存储排课结果。通过遍历课程列表,随机选择一个时间段,并将课程添加到排课结果中。同时,从时间段列表中移除已排课的时间段,以确保每个时间段只能排课一次。最后,打印出排课结果。
原文地址: https://www.cveoy.top/t/topic/qmn5 著作权归作者所有。请勿转载和采集!