💻 TechnologyGrade 6Lesson 27
← Back🔒 PDF with full access

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.

Trace this short program

What is x at the end? x = 5; x = x + 3; x = x * 2;

Answer:x = 16

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

  1. Read the error message — the line number is usually where the bug is.
  2. Trace the code by hand, writing the value of every variable on each line.
  3. Add a print statement before the broken line to see the real value.
  4. Check the index — is your loop going past the end of the list?
  5. 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.
Variable as a labeled boxVariable — One Name, One ValueA variable is a labeled box that holds a single value.score10speed5score = 10speed = 5Re-assigning replaces the value,not the name on the box.
List as boxes with indicesList — Many Values, One NameEach slot has an index, starting at 0.scores =010120230340[]scores[0] = 10, scores[2] = 30, scores[3] = 40scores[1] reads the value 20The last index is len(scores) - 1
💡

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.

Vocabulary
variable
A labeled box that holds a single value the program can read or change.
list
One name that stores several values in order, accessed by their position.
constant
A value the program promises never to change, written in CAPITAL_LETTERS.
index
The position of a value inside a list, starting from 0.
assign
To put a value into a variable using the equals sign.
trace
To read code line by line and record what every variable holds.
bug
A mistake in code that makes the program behave the wrong way.
debug
To find a bug, understand it, and fix it.
off-by-one
A bug where a loop or index is one too many or one too few.
Free · 7 days · No credit card

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
Start your 7-day free trial →
← Lesson 26: Conditions, Loops and EventsLesson 28: Make a mini-game or simulation →