0
Can sb tell me why this Code wrong is?
Question: You need to make a program to take a year as input and output "Leap year" if it’s a leap year, and "Not a leap year", if it’s not. To check whether a year is a leap year or not, you need to check the following: 1) If the year is evenly divisible by 4, go to step 2. Otherwise, the year is NOT leap year. 2) If the year is evenly divisible by 100, go to step 3. Otherwise, the year is a leap year. 3) If the year is evenly divisible by 400, the year is a leap year. Otherwise, it is not a leap year. My code: year = int(input()) if year % 4 == 0: if year % 100==0: if year % 400==0: print("Its a leap year!") else: print("Its not a leap year")
15 Answers
+ 4
I finally got it right:
year = int(input())
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print("Leap year")
else:
print("Not a leap year")
else:
print("Leap year")
else:
print("Not a leap year")
+ 2
Is it working really?
edit: oh. yes. it works..
other way, what I mean is:
if year%4 == 0 :
if year%100!=0 :
print("Leap year")
elif year%400==0 :
print("Leap year")
else : print("Not a leap year")
else :
print("Not a leap year")
+ 2
rules for checking if a year is a leap year:
Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. For example, the years 1700, 1800, and 1900 are not leap years, but the years 1600 and 2000 are.
(wikipedia)
+ 1
Inner if blocks has no else block, so may does not output anything if, if condition false. And logic must be year%100 != 0
if evenly divisable by 4 then must be either year not divisable by 100 or divisable by 400.
+ 1
Thank you guys
0
You actually don't need to put the second and third if statements, for an year to be a leap year, it just needs to be divisible by 4.
0
I already saw the other solution but I want to know how can I correct this in order to get the right answer.
0
For instance you take 2004 as input, it's obviously a leap year, but you're taking the conditions as the year must be divisible by 100 and 400 , then it's gonna show that 2004 is not a leap year. So, you need to remove those conditions as those are unnecessary for just accounting for a leap year.
0
2) & 3) else part is missing. Not covering otherwise, it is not a leap year.
0
But Diksha we should check for all three rules to verify the year as a leap year.
0
A leap year just needs to be divisible by 4, according to your code it will definitely work on years such as 1000, 2000, 4000, etc..but what about others like 2004,8,12..... .
0
Diksha Sharma
Check your code input 1900.
It prints leap year, but 1900 is not a leap year.
Aref Ahmadi
Actually, description may raise confusion, which can be removed if it should be like" If year is not evenly divisible by 100, then go to step 3. otherwise not a leap year. "
And your code missing else parts for inner if parts. If inner conditions fails then you don't get any output.
Are you tried these modifications ?
0
See the modifications now, it works.