Variables, lists and debugging
Lesson preview: the Mini Lesson and Vocabulary are open to everyone. Guided practice, exercises, the parent checklist and the printable PDF worksheet come with full access (free 7-day trial or membership).
Learning Objectives
- 1Define a variable as a labeled box that holds a single value
- 2Describe a list as one name that stores several values in order
- 3Trace how a value changes through a short sequence of code lines
- 4Spot and fix common bugs by reading the code one line at a time
Mini Lesson
Code is a recipe a computer follows step by step. To follow your recipe, the computer needs to remember things — a score, a name, a list of high scores. We give those memories a name so we can find them again. That name is called a variable, and a variable that holds many values in order is called a list.
Variable, List, Constant
- Variable — one name, one value, and the value can change. Example: score = 0, then later score = score + 1.
- List — one name, many values stored in order. Example: scores = [10, 20, 30]. The first value is at index 0.
- Constant — a value that should never change after you set it. Example: PI = 3.14 or MAX_LIVES = 3.
- Index — the position of a value inside a list, counted from 0. scores[0] is 10, scores[2] is 30.
Tracing — Read the Code Line by Line
When you trace, you act like the computer. You read each line in order and write down the new value of every variable. Tracing is the most important debugging skill — most bugs are bugs of expectation, where you think a variable holds one thing but it actually holds another.
What is x at the end? x = 5; x = x + 3; x = x * 2;
Line 1: x becomes 5. Line 2: x becomes 5 + 3 = 8. Line 3: x becomes 8 * 2 = 16. Always trace top to bottom.
Bugs and How to Squash Them
- Read the error message — the line number is usually where the bug is.
- Trace the code by hand, writing the value of every variable on each line.
- Add a print statement before the broken line to see the real value.
- Check the index — is your loop going past the end of the list?
- Compare what you typed to what you meant — typos and = vs == are common.
Common Bug Patterns
- Off-by-one — forgetting that lists start at index 0, so the last index is len(list) - 1.
- Using = (assign) when you mean == (compare). if x = 5 sets x; if x == 5 asks a question.
- Forgetting to update — you compute a new total but never write it back to the variable.
- Wrong type — adding a number to a string, e.g. 3 + "2", crashes or returns "32".
- Mutating a constant — changing PI or MAX_LIVES halfway through the program breaks every later line.
Tip: when a program does not behave the way you expect, add a print line just before the trouble. Print the variable. Nine times out of ten, the value is not what you thought, and the bug becomes obvious.
Unlock the full HSEA system for 7 days.
Add your first kid profile and your 7 days start: every lesson in every grade, saved progress for each child. Nothing to set up, no card required, nothing to cancel.
- ✓ All 1,680 lessons, Grades 1–6, every subject
- ✓ Profiles + saved progress for every kid
- ✓ Printable lessons with answer key