0

# What's wrong with this code

23.2

8 Answers

+ 3

G'day olli you were doing it different to everyone else, but not entirely wrong.... I like it!
Your first if eliminates all the years that aren't evenly divisible by 4, great idea.✅
Your next 2 elifs are in the wrong order, and one of them needs to say is a "leap year" with the other staying "not a leap year". They also need to be the more usual way, eg 👍elif year%400==0:
Your logic: eliminates the "not divisible by 4" then it needs to approve the divisible by 400, then eliminate the divisible by 100, then anything that passed all of those must be approved "a leap year".
Don't change your cool idea, just adjust your logic 😎
https://code.sololearn.com/cMnAF0MSp3FX/?ref=app

+ 3

olli
import calendar
print("Leap year" if calendar.isleap(int(input()) else "Not a leap year")
learning the basics is great, but learning the modules is also a good idea.

+ 2

olli
Without having access to the challenge description, we can only guess.
But I would guess this is a code to find leap years.
You might wish to review your understanding of % and play with the following concept
if year %4 ==0:

+ 2

# Logic:
for finding the leap year, you need to first find that the given year is a century year (like 1900, 1800, 2000) or a non century year (like 2001, 2016, 2005, 2012)
# century year is leap if it comes in every 400 years
# similarly a non century year is leap if it comes in every 4 years
# if it is a century year then it means it should be divisible by 400 (necessary condition) to be a leap year
# if it is non century year then it means it should be divisible by 4 and not divisible by 100.
example 1900 is divisible by 100. So not a leap year
https://code.sololearn.com/cBm31AI8C7vd/?ref=app

+ 1

Leap year
https://en.wikipedia.org/wiki/Leap_year

0

year = int(input())
if year % 4 > 0:
print ("Not a leap year")
elif year % 100 > 0:
print ("Not a leap year")
elif year % 400 > 0:
print ("Not a leap year")
else:
print ("Leap year")

0

You code is not working because you have designed it for only century years like 2000, 2100, 1900
You did not designed it for non century year like 2016, 2005, 2001
so your code is showing wrong answer when a non century year is entered.