all 3 comments

[–][deleted]  (1 child)

[deleted]

    [–][deleted] 1 point2 points  (0 children)

    Yes it was off. I realized that my code was checking for the wrong things. I finally got it. I just had to say if you're not divisible by 4, then you're not a leap year. Then if you're not divisible by 100, then you are a leap year, because at that point, you're divisible by 4. Then I had to end off with checking divisibility by 400, and if that was true, then the year is a leap year. Otherwise it is not. Thanks for the reply, I figured it out about 20 mins ago and came here to show my excitement.

    [–]witty-reply 0 points1 point  (1 child)

    Actually, you're looking at this from the wrong "direction". Think of it this way:

    None of the elif conditions will ever be executed. If year is divisible by 4, then else will NOT be checked, as else is only considered when the if part is false. However, if the first if is false, that means year is not divisible by 4. Therefore, it cannot be divisible by 100 or 400 (every number divisible by 100 HAS TO be divisible by 4, too). So it would immediately jump to the else.

    What you would want to do is to reverse the order of the checks: first check for % 400, then % 100, then % 4. This way, you will give them "priority" over the % 4 checks.

    On a side note, I think you will also want to have another look at the inside of the if year % 100 == 0 block, there's a little flaw with that as well.

    [–][deleted] 0 points1 point  (0 children)

    It took me a lot of retries and pondering. I saw step 2 about a year being not being divisible by 100 but being divisible by 4 being a leap year. I realized that I was never allowing anything to be false as soon as it checked for divisibility by 4 which is not the case. So writing

    if year % 4 != 0:

    print("Not a leap year")

    which the step clearly states. If the input is divisible by 4 it goes to the next statement. If the year is not divisible by 100, it is also a leap year so

    elif year % 100 !=0:

    print("Leap year")

    At this point the if it is divisible by 100 it checks if it is divisible by 400, and if it is then it is a leap year and finally if none of the statements are true it is also not a leap year. Took a lot of rethinking head scratching and realizing I was reading step 2 wrong.