栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 判断哪年哪月有几天

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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))

运行结果:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/849885.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号