+ 6
Where am I wrong?
I don't know why the test case 1 and test case 7 failed, please help. https://code.sololearn.com/c3DHegqiXTXG/?ref=app
11 Answers
+ 11
The 2nd , 3rd cases must go into nested if conditions for first condition. Not into else part.. Those are like compound conditions.
If divisable by 4 :
if divisable by 100 :
if divisable by 400: leap else not
else : leap
else : not
+ 9
Sakshi ,
may be it is easier to check when a year is not a leap year
(divisible means modulo division)
> if... it is not divisible by 4 => no leap year
> elif... it is divisible by 100 but not by 400 => no leap year
> else... all the other years are leap years => leap year
+ 6
Thank you everyone for solving my query and I solved it.
+ 5
Check year%400 before year%100, since year%100 is true when year%400 so the current logic will never check year%400.
+ 3
Leap year
https://en.wikipedia.org/wiki/Leap_year
+ 2
I know <DD> i can use 'or' 'and' operator but the challenge is in else statement, the task is this:-
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.
Sample Input
2000
Sample Output
Leap year
Use the modulo operator % to check if the year is evenly divisible by a number.
+ 2
Hello Sakshi! This was quite an interesting problem to me.. 🙂! Firstly, I thought that only modulo 4 was enough to chk a leap yr. But then I did some research and got why we need to chk the divisibility by 100 and 400 too! (I knew the rule earlier bt not in details with reason)
But that might not needed to be discussed in this thread.
So as u mentioned the qs above.. I'll go accordingly and focus on what u needed to do and what u actually did:
(Letz say year=y)
1. Check if y%4==0 and if itz true only then go to step 2 to chk with 100. You checked with 4 but inside the if block, u used the print command to output LeapYr. That means ur code will print leap year just when y%4==0, bt thatz not the case as only y%4==0 doesn't imply that the yr is leap! So u only need to indent and start the next if block under the 1st if block checking modulo 4. Like:
if y%4 ==0:
if...
And the otherwise option u can delay for the 'else' part of the 1st if block which will come at the very last part.
[Cont..]
+ 2
2. Check if y%100 ==0 and if itz true only then go to step 3 to chk with 400. Here u did chk but used print to output not leap yr when the chk result is true which is just the opposite to the step defined. U only can decide the input as a leapyr in this step if it's not evenly divisible by 100 i.e. y%100==0 is false. (U can chk this easily by using 'not'). And if the condition is true, you go to the next step.
3. Here u chk if y%400==0, this part u've done correctly as I see. But u've to ensure that this part is checked only after y%100==0 is true.
And finally u can have the else block where u can output for non leapyear. U might need 2 else blocks, one for the outer if and another for the inner.
I've edited ur code and it ran well.. 😊! I think if u've got the pts u can also do the tricks. Actually I'm a bit new here so I dunno if it's legal to share the whole program here! But if u need any further help just ask! 😀
+ 1
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")