Ms. Whitby's Classroom logo Ms. Whitby's Classroom

Algorithms & Testing — Lesson 1: Repeat until a stop signal

New skill: While loops and sentinel values
Previously learned: Units 01–09

Why this matters

Apps process a sequence of events until a logout, shutdown, or special signal.

Step 1 — Explore working Python

Read the program below. Before running it, write down the result you expect.

events = ['tap', 'tap', 'stop', 'tap']
index = 0
while index < len(events) and events[index] != 'stop':
    index += 1
print(index)

Step 2 — Trace the code

  1. Track the index that tells the loop which item to inspect.
  2. Use and so you never index past the end of the list.
  3. Stop before processing the special text 'stop'.

Step 3 — Check your understanding

How many events occur before 'stop' in ['a', 'b', 'stop', 'c']?

Step 4 — Watch for this mistake

A while loop can run forever when you forget to update the variable in its condition.

Practice: Repeat until a stop signal

Complete before_stop(events) to return how many values appear before the first 'stop'. If no stop signal appears, count the entire list.

Function contract: before_stop(events)
Starter file: main.py

Work in the editor, using Run to explore and Check to compare against the published examples. Your submitted code, not the public check, determines the recorded score.

Hint 1 — Start Begin an index or counter at zero.
Hint 2 — Build While the index is in bounds and the current value is not `'stop'`, move forward.
Hint 3 — Test Return the number of events visited.

Reflect

In 3–5 sentences, explain the algorithm you chose, one test case you predicted, and how you would detect an incorrect answer.

Optional reading

For another perspective on this skill, see How to Think Like a Computer Scientist (interactive textbook). This Playground lesson, worked example, and exercise are original; no textbook passages were reproduced.