判断当前的输入为当年的第几天

代码经过测试,个人感觉没有什么问题,但由于个人的经验不足以及眼光不够,希望各位指正程序中的不足之处。
附上代码

def check():
    '''
    输入一个日期,格式为xxxx-xx-xx,判断这一天为当年的第几天
    '''
    print('输入一个日期,格式为(xxxx-xx-xx):', end='')
    data = input()
    if '-' not in data or data.count('-') != 2:
        print('请按照正确格式输入年月日')
        return check()
    else:
        str1 = data.split('-')  
        year = int(str1[0])  # 年
        month = int(str1[1])  # 月
        day = int(str1[2])  # 日

        if month < 1 or month > 13:
            print('月输入错误,请重新输入')
            return check()
        elif month in day31:  # 月的天数为31天
            if day > 31 or day == 0:
                print('日输入错误,该月只有31天,请重新输入')
                return check()
            else:
                if year % year % 4 == 0 and year % 100 != 0 or year % 40 == 0:  # 判断是否为闰年
                    print(year366[month - 1] + day)
                else:
                    print(year365[month - 1] + day)
        elif month in day30:  # 月的天数为30天
            if day > 30 or day == 0:
                print('日输入错误,该月只有30天,请重新输入')
                return check()
            else:
                if year % year % 4 == 0 and year % 100 != 0 or year % 40 == 0:  # 判断是否为闰年
                    print(year366[month - 1] + day)
                else:
                    print(year365[month - 1] + day)
        elif month == 2:
            if ((day <= 28 or (year % 4 == 0 and year % 100 != 0 and day < 29) or (year % 400 == 0 and day <= 29))):
                if year % year % 4 == 0 and year % 100 != 0 or year % 40 == 0:  # 判断是否为闰年
                    print(year366[month - 1]+day)
                else:
                    print(year365[month - 1]+day)
            else:
                print('日输入错误,2月最多29天,或者该年不是闰年,请重新输入')
                return check()
if __name__ == '__main__':
    day31 = [1, 3, 5, 7, 8, 10, 12]     #天数为31天的月份
    day30 = [4, 6, 9, 11]               #天数为30天的月份
    year365 = [0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]     #平年每个月结束时的天数  365天
    year366 = [0,31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]     #闰年每个月结束时的天数  366天(2月多一天)
    check()

更新一个小错误,判断闰年的时候都是闰年,现已经更正。

def check():
    '''
    输入一个日期,格式为xxxx-xx-xx,判断这一天为当年的第几天
    '''
    print('输入一个日期,格式为(xxxx-xx-xx):', end='')
    data = input()
    if '-' not in data or data.count('-') != 2:
        print('请按照正确格式输入年月日')
        return check()
    else:
        str1 = data.split('-')  
        year = int(str1[0])  # 年
        month = int(str1[1])  # 月
        day = int(str1[2])  # 日

        if month < 1 or month > 13:
            print('月输入错误,请重新输入')
            return check()
        elif month in day31:  # 月的天数为31天
            if day > 31 or day == 0:
                print('日输入错误,该月只有31天,请重新输入')
                return check()
            else:
                if year % 4 == 0 :  # 判断是否为闰年
                    print(year366[month - 1] + day)
                else:
                    print(year365[month - 1] + day)
        elif month in day30:  # 月的天数为30天
            if day > 30 or day == 0:
                print('日输入错误,该月只有30天,请重新输入')
                return check()
            else:
                if year % 4 == 0 :  # 判断是否为闰年
                    print(year366[month - 1] + day)
                else:
                    print(year365[month - 1] + day)
        elif month == 2:
            if ((day <= 28 or (year % 4 != 0 and day < 29) or (year % 4 == 0  and day <= 29))):
                if year % 4 == 0 :  # 判断是否为闰年
                    print(year366[month - 1]+day)
                else:
                    print(year365[month - 1]+day)
            else:
                print('日输入错误,2月最多29天,或者该年不是闰年,请重新输入')
                return check()
if __name__ == '__main__':
    day31 = [1, 3, 5, 7, 8, 10, 12]     #天数为31天的月份
    day30 = [4, 6, 9, 11]               #天数为30天的月份
    year365 = [0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]     #平年每个月结束时的天数  365天
    year366 = [0,31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]     #闰年每个月结束时的天数  366天(2月多一天)
    check()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiao黄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值