Search code examples
pythonpython-3.xleap-year

correct my Python leap year code as per below conditions


  • The year can be evenly divided by 4, is a leap year, unless:
  • The year can be evenly divided by 100, it is NOT a leap year, unless:
  • The year is also evenly divisible by 400. Then it is a leap year.

Please correct my code. I'm not looking for alternatives. please use the above 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))

Solution

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