因為這週有事在忙,先把當初 Claude 寫的程式貼出來給大家參考。
-
這個寫法全部是 Claude 產生的(我只為了當題目,改了
content_cycle
裡「程、式、大、挑、戰」這幾個字),有些地方可能和一般的作法不同,但這是我和它交談後的成品。 -
使用
content_cycle
的好處:不管多少人輪值,不管輪幾天、不管是不是從頭開始,都可以順利成功。這個作法是我請 Claude 做的,它原本是用廻圈。 -
至於請使用者輸入年份,這個是它一開始就自己決定的。
-
它的答案,就是我之前所謂的半成品。再繼續請 Claude 修改的 effort,會比我多複製/貼上幾次的 effort 大,所以當時我就沒有繼續。(如果之後有空,我可能會為了當範例,請 Claude 做一個完整版試試)。
和 Claude 的問答,我有空再整理給大家參考。
from datetime import date, timedelta
def generate_calendar(year):
# 定義內容循環
content_cycle = ['程', '程', '程', '式', '式', '式', '大', '大', '大', '挑', '挑', '挑', '戰', '戰', '戰']
# 計算2024年9月1日到目標年份1月1日的天數
start_date = date(2024, 9, 1)
target_date = date(year, 1, 1)
days_diff = (target_date - start_date).days
# 計算目標年份1月1日的內容索引
content_start_index = days_diff % len(content_cycle)
# 生成整年的內容
year_content = []
for month in range(1, 13):
month_content = []
month_start = date(year, month, 1)
month_end = date(year, month + 1, 1) if month < 12 else date(year + 1, 1, 1)
# 填充月初的空白 (修正後的代碼)
month_content.extend([""] * ((month_start.weekday() + 1) % 7))
# 填充月份內容
current_date = month_start
while current_date < month_end:
index = (content_start_index + (current_date - target_date).days) % len(content_cycle)
month_content.append(content_cycle[index])
current_date += timedelta(days=1)
year_content.append(month_content)
# 生成日曆文本
calendar_text = ""
for month in range(12):
calendar_text += f"{year}年{month+1}月\n"
calendar_text += "日\t一\t二\t三\t四\t五\t六\n"
date_count = 1
for i in range(0, len(year_content[month]), 7):
week_content = year_content[month][i:i+7]
# 日期行
date_line = ""
for day in week_content:
if day == "":
date_line += "\t"
else:
date_line += f"{date_count}\t"
date_count += 1
calendar_text += date_line.rstrip() + "\n"
# 內容行
content_line = "\t".join(week_content)
calendar_text += content_line + "\n"
calendar_text += "\n" # 月份之間的空行
return calendar_text.strip()
def main():
while True:
try:
year = int(input("請輸入要生成日曆的年份(2025年或之後): "))
if year < 2025:
print("請輸入2025年或之後的年份。")
continue
break
except ValueError:
print("請輸入有效的年份。")
calendar_data = generate_calendar(year)
filename = f"{year}年日曆.txt"
with open(filename, "w", encoding="utf-8") as file:
file.write(calendar_data)
print(f"日曆資料已保存到 {filename}")
if __name__ == "__main__":
main()