[Python] 计算下一天

该代码定义了一个next_day函数,它接收一个日期(年/月/日)作为输入,通过is_leap_year函数判断年份是否为闰年,然后计算出输入日期的下一天。在闰年中,二月有29天,其他月份的天数处理也相应考虑了。
摘要由CSDN通过智能技术生成

描述

补充函数next_day代码给出输入日期的下一天,is_leap_year用于判断给定年份是否为闰年。

1

# 闰年判定:
# 1.能被400整除
# 2.不能被100整除但是可以被4整除
def is_leap_year(year):
    if year % 400 == 0:
        return True
    elif year % 100 == 0:
        return False
    elif year % 4 == 0:
        return True
    else:
        return False


def next_day(year, month, day):
    if is_leap_year(year) == True:
        if month == 1:
            if day == 31:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
            return year, month, day
        elif month == 2:
            if day == 29:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 3:
            if day == 31:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 4:
            if day == 30:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 5:
            if day == 31:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 6:
            if day == 30:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 7:
            if day == 31:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 8:
            if day == 31:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 9:
            if day == 30:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 10:
            if day == 31:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 11:
            if day == 30:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 12:
            if day == 31:
                day = 1
                month = 1
                year += 1
            else:
                day += 1
                return year, month, day
    else:
        if month == 1:
            if day == 31:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
            return year, month, day
        elif month == 2:
            if day == 28:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 3:
            if day == 31:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 4:
            if day == 30:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 5:
            if day == 31:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 6:
            if day == 30:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 7:
            if day == 31:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 8:
            if day == 31:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 9:
            if day == 30:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 10:
            if day == 31:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 11:
            if day == 30:
                day = 1
                month += 1
                return year, month, day
            else:
                day += 1
                return year, month, day
        elif month == 12:
            if day == 31:
                day = 1
                month = 0
                year += 1
            else:
                day += 1
                return year, month, day


year, month, day = map(int, input().split('/'))
year, month, day = next_day(year, month, day)
print(f"The next day is {year}-{month}-{day}.")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值