# Shift Rotation Schedule python code by Chatgpt 4o with Canvas (prompt by Kenny Huang)

**URL:** https://vip.studycamp.tw/t/shift-rotation-schedule-python-code-by-chatgpt-4o-with-canvas-prompt-by-kenny-huang/7577
**Category:** AI 程式大挑戰
**Created:** [2024年十月7日 08:12 UTC](https://vip.studycamp.tw/t/shift-rotation-schedule-python-code-by-chatgpt-4o-with-canvas-prompt-by-kenny-huang/7577 "2024-10-07T08:12:12Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Kenny\_Huang](https://vip.studycamp.tw/user_avatar/vip.studycamp.tw/kenny_huang/32/5005_2.png) [@Kenny\_Huang](https://vip.studycamp.tw/u/Kenny_Huang)
#### Post date: [2024年十月7日 08:12 UTC](https://vip.studycamp.tw/t/shift-rotation-schedule-python-code-by-chatgpt-4o-with-canvas-prompt-by-kenny-huang/7577/1 "2024-10-07T08:12:12Z")

</div>

```python
import datetime

def create_shift_schedule(start_date, names, filename, end_date):
    current_date = datetime.datetime.strptime(start_date, "%Y-%m-%d")
    end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d")
    schedule = []
    
    # Start with "程" for the first day
    schedule.append(f"{current_date.strftime('%Y-%m-%d')}: {names[0]}")
    current_date += datetime.timedelta(days=1)
    
    names_len = len(names)
    shift_index = 1 # Start from the second person "式"
    while current_date <= end_date:
        for _ in range(3): # 每人連續輪值三天
            if current_date > end_date:
                break
            schedule.append(f"{current_date.strftime('%Y-%m-%d')}: {names[shift_index]}")
            current_date += datetime.timedelta(days=1)
        shift_index = (shift_index + 1) % names_len
    
    with open(filename, 'w', encoding='utf-8') as f:
        for entry in schedule:
            f.write(entry + '\n')

create_shift_schedule("2025-01-01", ["程", "式", "大", "挑", "戰"], "shift_schedule.txt", "2025-12-31")
print("排班表已完成並保存至 shift_schedule.txt 文件。")

```

---

<div class="post-metadata">

### Author: ![AI\_Challenge](https://vip.studycamp.tw/user_avatar/vip.studycamp.tw/ai_challenge/32/7869_2.png) [@AI\_Challenge](https://vip.studycamp.tw/u/AI_Challenge)
#### Post date: [2024年十月7日 08:27 UTC](https://vip.studycamp.tw/t/shift-rotation-schedule-python-code-by-chatgpt-4o-with-canvas-prompt-by-kenny-huang/7577/2 "2024-10-07T08:27:10Z")

</div>

💗 感謝共襄盛舉參與本活動 💗

可能是題目說明的不夠清楚，這裡有半成品範例（txt 檔，會從這裡複製貼上到 xls 檔中）供您參考：

```txt
▌2025年1月
日	一	二	三	四	五	六
			1	2	3	4
			程	式	式	式
5	6	7	8	9	10	11
大	大	大	挑	挑	挑	戰
12	13	14	15	16	17	18
戰	戰	程	程	程	式	式
19	20	21	22	23	24	25
式	大	大	大	挑	挑	挑
26	27	28	29	30	31
戰	戰	戰	程	程	程

▌2025年2月
日	一	二	三	四	五	六
						1
						式
2	3	4	5	6	7	8
式	式	大	大	大	挑	挑
9	10	11	12	13	14	15
挑	戰	戰	戰	程	程	程
16	17	18	19	20	21	22
式	式	式	大	大	大	挑
23	24	25	26	27	28
挑	挑	戰	戰	戰	程

```
