Python简单示例-输入年份月份输出日历

用户输入年份和月份,年份不能小于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('')

输出格式与结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值