Python 判断哪年哪月有几天

# 判断哪年哪月有几天

"""
介绍:
    普通闰年:公历年份是4的倍数,且不是100的倍数的,为闰年(如2004年、2020年等就是闰年)。
    世纪闰年:公历年份是整百数的,必须是400的倍数才是闰年(如1900年不是闰年,2000年是闰年)。
    平年与闰年的区别:闰年的2月是29天,而平年的2月是28天;闰年一年有366天,平年一年有365天。

知识点:
    1、数据类型转换
    2、while 循环语句
    2、if/elif/else 条件语句
"""


while True:

    # 带提示语输入赋值
    year = float(input('请输入年份:'))
    month = float(input('请输入月份:'))

    # 重新输入
    if year < 1 or month < 1 or month > 12 or year > int(year) or month > int(month):
        print('输入有误!!!')
        continue

    # 数据类型转换
    year = int(year)
    month = int(month)

    # 平年/闰年共有的月份
    if month in [1, 3, 5, 7, 8, 10, 12]:
        print('{}年/{:0>2}月,有31天' .format(year, month))

    elif month in [4, 6, 9, 11]:
        print('{}年/{:0>2}月,有30天'.format(year, month))

    # 特殊的2月份,单独处理
    elif month == 2:

        # 判断是否为闰年,世纪闰年和普通闰年一起判断
        if year % 400 or (year % 4 == 0 and year % 100 != 0):
            print('{}年/{:0>2}月,有29天'.format(year, month))

        else:
            print('{}年/{:0>2}月,有28天'.format(year, month))

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值