根据以下条件更正我的 Python 闰年代码

correct my Python leap year code as per below conditions

请更正我的代码。我不是在寻找替代品。请使用以上条件

def is_leap(year):
    nonleap = False
    leap = True
    x = year % 4
    y = year % 100
    z = year % 400
    if(x == 0):
        if((y % 2 != 0) or ((y % 2 == 0) and (z % 2 == 0))):
            return leap
    else:
        return nonleap

year = int(input())
print(is_leap(year))

您的代码存在问题,您正在检查 y 和 z 是否可以被 2 整除。

使用您的代码作为基础的解决方案可能如下所示

def is_leap(year):
    nonleap = False
    leap = True
    x = year % 4
    y = year % 100
    z = year % 400
    if x == 0:
        if (y == 0) and not (z == 0):
            return nonleap
        else:
            return leap
    else:
        return nonleap

但这里有一个更优雅的pythonic解决方案 https://www.hackerrank.com/challenges/write-a-function/forum

def is_leap(year):
    return year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)
def is_leap(year):
    x = year % 4
    y = year % 100
    z = year % 400
    if(x == 0):
        if((y != 0) or ((y == 0) and (z == 0))):
            return True
        else:
            return False
    else:
        return False

year = int(input())
print(is_leap(year))