Python-打印年度日历小程序
作者:互联网
1 year = int(input('请输入年份: ')) 2 n = year//4 - year//100 + year//400 #计算从公元元年开始到本年度的闰年数量 3 t = (year + n ) % 7 #计算本年度1月1号是星期几 4 5 dnum = [31,28,31,30,31,30,31,31,30,31,30,31] 6 if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: 7 dnum[1] = 29 #闰年二月份是29天 8 9 print(f'年份:{year}') 10 11 for i in range(0,12): 12 print(f'{i+1}月') 13 print('一\t二\t三\t四\t五\t六\t日') 14 for k in range(t-1): 15 print(' ', end='\t') #打印每月前面的空白部分 16 r = 7 - t; 17 for j in range(dnum[i]): 18 if t % 7 == 0 : #是否换行判断 19 print(f'{j+1:2}') 20 t=1 21 else: 22 print(f'{j+1:2}', end='\t') 23 t = t + 1 24 25 if j % 7 != r : #月底是否需要换行 26 print(' ')
标签:日历,Python,30,31,打印,dnum,range,year,print 来源: https://www.cnblogs.com/fengye2021/p/15086144.html