Algorithms & Testing — Lesson 2: Break a problem into helper functions
New skill: Decomposition, pure functions
Previously learned: Units 01–09
Why this matters
Large apps become easier to debug when their calculation rules are separated into reusable pieces.
Step 1 — Explore working Python
Read the program below. Before running it, write down the result you expect.
def nonnegative(value):
return max(0, value)
def total_points(scores):
total = 0
for score in scores:
total += nonnegative(score)
return total
print(total_points([8, -2, 5]))
Step 2 — Trace the code
- Read the helper’s contract: negative numbers become zero.
- Trace how the main function calls the helper for each item.
- Combine helper results into a total rather than writing one giant expression.
Step 3 — Check your understanding
What does total_points([-2, -1, 7]) return?
Step 4 — Watch for this mistake
Do not forget the return statement in either function. A missing return produces None.
Practice: Break a problem into helper functions
Complete total_with_bonus(scores, bonus) to sum only positive scores, then add bonus. Zero and negative scores contribute nothing. You may create helper functions.
Function contract: total_with_bonus(scores, bonus)
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
Start a total at zero.Hint 2 — Build
Only add a score when it is greater than zero.Hint 3 — Test
Return the total plus the bonus after the loop.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.