Now everything you've built comes together. A function that takes a collection, loops through it, and returns a result is the shape of nearly all real software.
EXAMPLE
loading editor…
YOUR TURN
Your turn → Parameters + loop + accumulator + return, in one reusable unit. Call it on a second, different crew without changing the function.
loading editor…
Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.
Composition is the real reward: functions calling other functions, each doing one job well.
EXAMPLE
loading editor…
YOUR TURN
Your turn → average_age doesn't recompute the sum, it CALLS total_ages. Each function does one job. Add a max_age function and compose with it.
loading editor…
Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.
✦ Naming functions
A good function name is a verb phrase describing what it returns or does: total_ages, average_age, is_eligible, find_oldest. If you can't name it in a short phrase, it's probably doing too many things. Split it.
◈ Graded Challenge
Write two functions. count_members(members) returns how many records are in the list, and total_hours(members) returns the sum of their hours. Both must RETURN, never print.
Name it exactly: def count_members(members) and def total_hours(members)
3 visible + 2 hidden
loading editor…
Mark this lesson complete
Signed in, your progress follows you to every device.