Skip to content
PlayOwl

Coding worksheet · Coding

Trace the loop: what is printed? (stretch) · Grades 9–10

Read 7 short Python programs about for, while and nested loops and work out by hand what each one prints or the final value of a variable. Builds careful code tracing.

  • Grades 9–10 · ages 14 to 16
  • Hard
  • 30 to 40 minutes
  • 1 page
  • Free
Paper

The answer key is a separate page for grown-ups. Sign in to download it.

Make another one like this

Directions: Trace each program line by line without a computer. Keep a note of every variable as it changes, then write exactly what the program prints or the final value.

  • 1 What is printed?
  • 1 def times_four(n):
  • 2 return n * 4
  • 3 total = 0
  • 4 for i in range(1, 3):
  • 5 total += times_four(i)
  • 2 What is printed?
  • 1 nums = [29, 24, 11, 23, 22]
  • 2 least = nums[0]
  • 3 for n in nums:
  • 4 if n < least:
  • 5 least = n
  • 6 print(least)
  • 3 What is printed?
  • 1 values = []
  • 2 for i in range(4):
  • 4 print(values)
  • 4 What is printed?
  • 1 n = 9813
  • 2 total = 0
  • 3 while n > 0:
  • 4 total += n % 10
  • 5 What is printed? Write each line.
  • 1 for i in range(3):
  • 6 What is printed?
  • 1 text = "turtle"
  • 2 result = ""
  • 3 for ch in text:
  • 4 result = ch + result
  • 5 print(result)

What your child practices

Your child builds careful code tracing. The skill is “Loops”: use repetition to shorten programs.

Grades and ages
Grades 9–10, ages 14 to 16
Time
About 30 to 40 minutes
Subject
Coding
Level
Hard for Grades 9–10
Answer key
Yes, for grown-ups who sign in
Printing
1 page, US Letter or A4, low-ink option
Updated

How to use it

Work through the first program together, one line or arrow at a time. Then let your child work through the rest, showing their thinking on the page. It is one of the harder sheets for Grades 9–10, best once the basics of the skill are secure.

An answer key comes with it as a separate page that grown-ups download after signing in.

Tip for grown-ups: Have your child write each variable's value beside its line as they trace.

Done on paper? A grown-up can record it on the child's page in PlayOwl; downloading alone doesn't count as finished.

Printable generator

Make your own printables

Make brand-new sheets like this one, with answer keys, using the printable generator that comes with a PlayOwl plan. This sheet stays free.