← Back to course
Code Crew G4 / Week 03 / Crew Quest
6/6
Week 03 Β· Loop through a list

Crew Quest

Crew Quest: Loop Walk
// Visit every item
⏱ about 15 min

Crew Quest: Loop Walk

The station gym has a long row of floor tiles. Raven puts a paper number on each one. "This is a list," she says. "What do you notice if we walk along it?"

Rocket hops onto the first tile. "Twelve!" he calls. Then he hops to the next one. "Eight! That makes twenty so far."

Nova hovers at the end of the row and flashes. "You are a for loop with a running total," she says. "Hop, add, repeat. Then say the total when you reach me."

  1. A for loop visits each item in a list, one at a time.
  2. Indented lines are inside the loop. They run once for each item.
  3. The first line with no indent runs once, after the loop.
  4. A running total starts at 0 and adds each value.
  5. A counter with an if counts only the items that match. == asks if two values are the same.
β—‡ LOOP WALK
Write five small numbers on scraps of paper.
Lay them on the floor in a row. This is your list.
Walk along the row. Say the running total out loud at each step.
Now walk again. This time count only the numbers bigger than 5.
Swap jobs. Your partner walks while you check the total.
steps = [3, 7, 2, 8]
total = 0
for s in steps:
    total = total + s
print(total)
What number does the loop walk program show?
WHY THIS EXERCISEWalking a loop with your body is a great way to trace it.
BONUS RECAP
  • Read the question.
  • Tap your answer.
A loop prints each item of [1, 2, 3]. How many lines show?
A total starts at 0. A loop adds 4, then 6. What is the total?
Which sign asks if two values are the same?

Fantastic week! Next week, your loops will make choices.

← Friday