Python 函数实现月历打印 - 1900年到9999年
def Calendar(year, month): '计算当月第一天是星期几' days_in_month = [31, 28 + (year % 4 == 0 and year % 100 != 0 or year % 400 == 0), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] days = 0 for i in range(1, month): days += days_in_month[i - 1] days += (year - 1900) * 365 + (year - 1901) // 4 - (year - 1901) // 100 + (year - 1601) // 400 weekday = (days + 1) % 7
'输出月历'
print(' Mo Tu We Th Fr Sa Su')
for i in range(weekday):
print(' ', end='')
for i in range(1, days_in_month[month - 1] + 1):
print('{:>2}'.format(i), end='')
weekday = (weekday + 1) % 7
if weekday == 0 and i != days_in_month[month - 1]:
print(' ')
print()
原文地址: https://www.cveoy.top/t/topic/m3oT 著作权归作者所有。请勿转载和采集!