以下是一个Python函数,用于判断给定的年份是否为闰年:

def is_leap(year):
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                return True
            else:
                return False
        else:
            return True
    else:
        return False

该函数首先检查年份是否能被4整除。如果可以,接下来检查年份是否能被100整除。如果能被100整除,再检查年份是否能被400整除。只有当年份能被400整除时,才是闰年。如果年份不能被100整除,但能被4整除,也是闰年。否则,年份不是闰年。

你可以通过调用该函数并传入一个年份来判断该年份是否为闰年,例如:

print(is_leap(2020))  # 输出 True
print(is_leap(2021))  # 输出 False
``
以python定义一个is_leapyear函数该函数可判断year是否为闰年。若是闰年则返回True;否则返回False。

原文地址: http://www.cveoy.top/t/topic/hSbZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录