下面是一个简单的Python程序,可以输出指定年份和月份的万年历:

import calendar

year = int(input("请输入年份:"))
month = int(input("请输入月份:"))

cal = calendar.monthcalendar(year, month)

print("{0}年{1}月".format(year, month))
print(" Mo Tu We Th Fr Sa Su")

for week in cal:
    line = ""
    for day in week:
        if day == 0:
            line += "   "
        else:
            line += "{0:2} ".format(day)
    print(line)

使用示例:

请输入年份:2022
请输入月份:11
2022年11月
 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

该程序使用了Python标准库中的calendar模块,其中的monthcalendar()函数返回指定年份和月份的日历列表,每个元素代表一周的日期(0表示该日期非本月的),并且每个元素都使用了两个字符宽度来输出

python中万年历

原文地址: https://www.cveoy.top/t/topic/fJAE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录