Lesson 8 — Testing AI code with assert
Make Python check for you
assert checks that something is true. If it is, nothing happens. If it is not, Python raises an AssertionError with your message:
assert is_leap_year(2024) == True, "2024 should be a leap year"
A test is a small check like this with an input and the answer you know is right. Good tests include the tricky cases. For leap years the rule is:
- divisible by 4 → leap year,
- except divisible by 100 → not a leap year,
- except divisible by 400 → leap year again.
So 1900 is not a leap year, but 2000 is. The AI answer below only checks the first rule, and the easy tests (2024, 2023) still pass. That is why you need the tricky ones.
Why it matters: "it worked for my example" proves very little. A few well-chosen asserts prove much more.
Add check(1900, False) and check(2000, True). One will FAIL — then fix is_leap_year() so all four tests PASS.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.