Python def function return None in all cases

Your code

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


print(leap(1992))

Let’s break it down for 1992.

  1. year = 1992.

    1992 % 4 = 0. True. The code moves to next if statement.

    1992 % 100 = 92(False). so your codes halts at this point and exits from the function. Hence the None output for 1992 since you haven’t added any logic to return false here.

A simple approach to avoid the None output would be.

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


print(leap(1992))

Leave a Comment