Concept Explanation
This lesson introduces a habit that good developers build early: checking small pieces of code before they become bigger problems. You are not learning full test frameworks yet. You are learning how to ask your code simple questions and confirm that the answers stay correct. In Python, `assert` is one of the easiest ways to do that. It lets you write a short rule such as "this value should never be negative" or "this function should return 42 for this input". If the rule fails, Python stops and shows you exactly where your assumption was wrong. That matters because beginners often rely on "it looks right" instead of verifying behavior. Testing changes that. It gives you a fast way to catch mistakes while the code is still small and easy to fix. By the end of this lesson, you should be able to: • write a few basic assertions for a small helper function, • understand what an `AssertionError` is telling you, • use quick checks to confirm your code still works after a small change. Why this matters: once you start changing code more often, confidence becomes important. Small tests give you that confidence without making the lesson feel heavy or complicated.
Where to Put the Code
- Define color and position variables at the top.
- Create shape drawing or placement logic in the middle.
- Render output (print, canvas, SVG, or styled block) at the end.
Command Reference
- Use `assert` for small learning checks when you want to confirm that a function returns the value you expect.
- Keep each assertion focused on one behavior so it is easy to understand what failed.
- Run your checks after every small edit instead of waiting until the lesson is finished.
- Treat failing assertions as useful feedback, not as a sign that you are bad at coding.
Step-by-step Guide
- Run the file once and confirm that `All score checks passed.` appears in the terminal.
- Change one expected value on purpose so an assertion fails, then read the error and fix it.
- Add a new assertion for `normalize_score(0)` and run the file again.
- Add another assertion for `normalize_score(100)` so you test both edges of the valid range.
- Write one short sentence explaining why quick checks are helpful before a script becomes larger.
Practice Exercises
- Create a second helper function that clamps a temperature value into a safe range, then write two or three assertions for it.
- Change the function so it returns the wrong value for one case and confirm that your assertions catch the mistake.
- Describe in your own words the difference between "the code runs" and "the code is correct for the cases I tested".
Coding Challenges
- Write a version of the function that raises `ValueError` for impossible inputs instead of silently fixing them, then decide which style feels clearer for this lesson.
- Create a tiny test function for a string helper, such as trimming spaces or capitalizing a name, and make sure every assertion is easy to read.
Mini Practice Tasks
- Run the file three times after small edits so testing starts to feel normal.
- Keep one failed assertion on purpose for a minute, read it carefully, then repair it.
- Write one note to yourself: `Small tests help me trust my code.`
Common Mistake
Mixing x and y axes or using wrong coordinate origin causes shapes to appear in unexpected places.
Real-life Mini Challenge
Draw one square, one triangle, and one circle, then move X marker 2 steps right and 1 step down.