Ask coding questions

← Back to all posts
I don't think this is working right but I am on it for quite a bit now
CapnDom

I am trying to do a course, and the solution is entirely different than this. Where is it going wrong? Any pointers? 2400 should be a leap year, but it says it's not

Voters
CapnDom
Comments
hotnewtop
SixBeeps

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

In a structured list-like form:

  • The year is divisible by 4:
    • The year is divisible by 100:
      • The year is divisible by 400: YES
      • Otherwise: NO
    • Otherwise: YES
  • Otherwise: NO

What you have right how is:

  • The year is divisible by 4:
    • The year is ALMOST divisible by 100: YES
    • The year is divisible by 400: YES (Note how if a number is divisible by 400, it is also divisible by 100.)
    • Otherwise: NO
  • Otherwise: NO

So you might want to rethink how your statements are structured.

CapnDom

@SixBeeps Thanks, I don't know. I tried something, it didn't work with the syntax and then I tried to go around that and ended up confusing myself. I should have restarted earlier. In a minute I will give it another go in a clean, new repl just to try.

CapnDom

@SixBeeps Okay, after a little bit of a break it was very easy. I don't know what sort of brainfart I had there, but it seemed so overly complicated. Can you believe I sat 2 hours on it?

I now wrote the code in 2 MINUTES, and I think it's good and works. Wanna look over it?

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.")

(forgive formatting, they're properly indented.)