close
import calendar
# 輸入年份和月份
year = int(input("請輸入年份:"))
month = int(input("請輸入月份:"))
# 產生月曆
cal = calendar.monthcalendar(year, month)
# 輸出月曆
print(calendar.month_name[month], year)
print("Mo Tu We Th Fr Sa Su")
for week in cal:
for day in week:
if day == 0:
print(" ", end="")
else:
print("{:2d}".format(day), end=" ")
print()
輸出結果會像這樣:
請輸入年份:2023
請輸入月份:3
March 2023
Mo Tu We Th Fr Sa Su
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
程式碼中使用了Python標準庫中的calendar模組,該模組提供了一些日曆相關的函式和常量,例如monthcalendar函式可以用來產生指定年份和月份的月曆。
全站熱搜