用户输入年份和月份,年份不能小于1900年,按格式输出日历
year = int(input('请输入年份:\t')) #输入年份,需要将input的年份(str)转换为int
month = int(input('请输入月份:\t')) #输入月份
day=0
total=0
space=0
#判断输入的年份是否是闰年
isRun = year%4==0 and year%100!=0 or year%400==0
#计算1900到输入年份之前的所有天数
for y in range(1900,year):
if y%4==0 and y%100!=0 or y%400==0:
total+=366
else:
total+=365
#计算输入月份之前到输入年份之前的所有天数
for m in range(1,month+1):
if m==1 or m==3 or m==5 or m==7 or m==8 or m==10 or m==12:
day=31
elif m==2:
if isRun==True:
day=29
else:
day=28
else:
day=30
if m<month:
total+=day
#计算输入月份第一天的空格数,及获取是星期几
space=total%7+1
if space==7:
space=0
print('日\t一\t二\t三\t四\t五\t六')
for s in range(0,space):
print('\t',end='')
for d in range(1,day+1):
print('{}\t'.format(d),end='')
# 判断是否为周六并换行
if(total+d)%7==6:
print('')
输出格式与结果: