We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

I thought the constraints meant that you could write your code assuming the constraints to be true - that no test cases would include inputs that don't satisfy the constraints.

Reversing the order almost works, but you have to add an else block returning False at the end to capture cases where none of the if blocks execute (like for the year 1992) and also change all elif statements to if statements. I.e:

Why wouldn't 1992 return true if % 4 == 0 is the first statement? it divides cleanly by 4 with no remainder, yet it gives false in the original scenario.

because for (year/4, year/100, year/400), you need to insert the answer.
And you cannot do that because you do not know all the values for the
year variable. If you were to hardcode any year, for instance,

It works... can u explain
a=(False)
b=(True)
if year in range(1900,pow(10,5)):
if (year%(4 or 400))!=0:
return a
if (year%100)==0:
return a
else:
return b
in test case 5 it fails

## Write a function

You are viewing a single comment's thread. Return to all comments →

This is what i did.

Thanks for explaining

the first if statement if year%4==0: return True this will give true for 1900 year also but that it is not true

you have to give the constraint like

if year in range(1900,pow(10,5)+1): because they clearly mentioned this constraintr

I thought the constraints meant that you could write your code assuming the constraints to be true - that no test cases would include inputs that don't satisfy the constraints.

that is true

You can just reverse the order and it will be fine.

Reversing the order almost works, but you have to add an

`else`

block returning`False`

at the end to capture cases where none of the`if`

blocks execute (like for the year 1992) and also change all`elif`

statements to`if`

statements. I.e:This passes all the test cases.

i will mark your words

not passes year %100 == 0 test

Why wouldn't 1992 return true if % 4 == 0 is the first statement? it divides cleanly by 4 with no remainder, yet it gives false in the original scenario.

true

why we have to change elif statements to if statements sir??

i did the same thing but it's not working for year 2100 2100 is a leap year,but according to the compiler we should get False.

will u please explain this why if not elif works

why is it % and not / ???

because for (year/4, year/100, year/400), you need to insert the answer. And you cannot do that because you do not know all the values for the year variable. If you were to hardcode any year, for instance,

year = 2000 if year/4 == 500: return True

I reversed the order and it passed all the tests. But i dont know why it did. what did the reversing the order do??

Took a few tries before I understood the conditions but I got it!

Here's my solution:

That wound up failing two test cases.

how can you return heap without any function

This quote

isthe function. The post doesn't include the declaration because it's not relevant.fail in two cases

What about trying something like this:

this works for all test cases

It fails for the year 2100

It is giving runtime error

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

make the last loop as elif year%100!=0: return True

this will work for all the cases

Good Work ！ I think I ignored the "unliess" in the problem sheet. I learned the if and elif, but never used them. This is an interesting question.

I did something pretty much the same but it failed one.

This is what I did.

year = int(input())

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

year = int(input()) print(is_leap(year))

why it is giving error on input 2100

I have the Same Error..what to do?

It works... can u explain a=(False) b=(True) if year in range(1900,pow(10,5)): if (year%(4 or 400))!=0: return a

if (year%100)==0: return a else: return b in test case 5 it fails

ran your code, 3 out of 6 test cases failed :(

ohh ... halo

where is else block

check for 1990 with your code

if i am enter the 2100 year than it give ans True which is not True plz help me