-3
def is_leap(year):
    leap = False
    
    # Write your logic here
    if year%4==0:
        return True
    elif year%400==0:
        return True
    elif year%100 != 0:
        return False
    else:
        return False
    return leap


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

It is showing that one test case is failing.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • 4
    How would you ever reach `year%400 == 0` check and have it be True if `year%4 == 0` check wasn't True? Every number divisible by 400 is also divisible by 4. – matszwecja Feb 01 '23 at 13:51
  • Wondering if `year %400` is not redundant. Secondly, you didn't specify which is failing. Recommendation: perhaps first write in algorithm as algorithm and then put as Python code – semmyk-research Feb 01 '23 at 13:55
  • @semmyk-research it's not with properly written conditions, as years divisible by 100 are not leap years unless they are also divisible by 400. – matszwecja Feb 01 '23 at 13:58

3 Answers3

1

Pay attention to the order. Your code second condition year%400==0 cannot ever be reached, as anything that can be divided by 400 can also be divided by 4. Hence, every multiplication of 400 would have already been caught in the year$4==0 condition

def is_leap(year):
    if year % 400 == 0:
        return True
    if year % 100 == 0:
        return False
    if year % 4 == 0:
        return True
    return False
tzadok
  • 353
  • 3
  • 12
0

Remember that conditions are evaluated in the order they are written.

if year%4==0:
    return True

Every year divisible by four is a leap year...

elif year%400==0:
    return True

and every year divisible by 400 is also divisible by four, so this condition is never evaluated

elif year%100 != 0:
    return False

All other years not divisible by 100 are not leap years.

else:
    return False

And everything else is also not a leap year.

There is no point in having these two last cases; if condition: return False else: return False is equivalent to return False.

In summary: your conditional is equivalent to

if year%4==0:
    return True
else:
    return False

or simply return year % 4 == 0.
I hope you agree that this is incorrect.

Let's define a leap year.

The year X is a leap year if

  • It is divisible by four,
  • unless it is divisible by 100,
  • except if it is divisible by 400

That is,

return x % 4 == 0 and not (x % 100 == 0 and not (x % 400 == 0))

which we can simplify,

return x % 4 == 0 and not (x % 100 == 0 and x % 400 != 0))

One of DeMorgan's laws: not (a and b) == (not a) or (not b):

return x % 4 == 0 and (x % 100 != 0 or x % 400 == 0)
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

Essentially, your use of if and elif in the order you have, renders year%400==0 redundant. The if year%4==0 will be true, and the test condition stops.

I'll recommend you start with an algorithm and flowchart, then put in python code.

So, what's a leap year?
[Wikipedia!] A year that is an integer multiple of 4 (except for years evenly divisible by 100, but not by 400)

Back to your code: Need to test!
Note the brackets around the conditions and the use of and
[Updated: realised I posted the wrong code snippet]

# To write your logic here
# First, define leap year
### divisible by 4 and if divisible by 100, also divisible by 400
# Work out the logic and then write the code

def is_leap(year): 
    ## set leap default to false
    leap = False

    ## leap condition
    ## divisible by 4 except if divisible by 100 but not divisible by 400, return true (as leap year)
    ## the `and not` implies 'except'
    if (year%4 == 0) and not (year%400 != 0 and year%100 == 0):
        return True
    else:
        return False
    return leap

'''
## Note that we can follow the flowchart testing for 4, except (100 and not 400)
## Possible one-liner
    return (year % 4 == 0) and not (year % 100 == 0 and year % 400 != 0)
'''

year = int(input()) 
#year2 = 1900 #test
print(f'{year2} is leap year: {is_leap(year2)}')
semmyk-research
  • 333
  • 1
  • 9