2 views
in Python

I can't for the life of me understand this test on Hackerrank. The nearest I got it was to 4/6 passes. Rules: In the Gregorian schedule three standards should be considered to recognize jump years:

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.

code:

def is_leap(year):

leap = False

if year%400==0 :

leap = True

elif year%4 == 0 and year%100 != 0:

leap = True

return leap

year = int(input())

print(is_leap(year))

by (26.4k points)

You failed to remember the ==0 or !=0 which will help comprehend the conditions better. You don't need to utilize them, yet then it can create turmoil keeping up the code.

def is_leap(year):

leap = False

if (year % 4 == 0) and (year % 100 != 0):

# Note that in your code the condition will be true if it is not..

leap = True

elif (year % 100 == 0) and (year % 400 != 0):

leap = False

elif (year % 400 == 0):

# For some reason here you had False twice

leap = True

else:

leap = False

return leap

You can also try this code (smaller version):

def is_leap(year):

return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

Want to be a python expert? Come and join this python course