Pass Values and Return a Result
Turn the fixed summary into mean(readings), bind an argument to its parameter, and return either a numerical mean or None when no reading is available.
show_summary() always uses the readings written inside its body. To summarize
a different measurement, the function needs an input supplied by its caller.
Pass Readings into the Function
Put the name readings between the parentheses in the definition:
Here readings is a parameter: a local name that receives an input for one
call. A call supplies an argument:
For this call, the value referred to by morning_readings is passed to the
parameter readings. The function performs the familiar loop with that value.
Return the Calculated Result
The return statement sends a value back to the code that made the call. Trace
the morning call from input to result:
| Step | Current reading | total | count | Result sent to caller |
|---|---|---|---|---|
bind argument to readings | — | — | — | — |
| initialize summary | — | 0 | 0 | — |
| process 18 | 18 | 18 | 1 | — |
| process 21 | 21 | 39 | 2 | — |
| process 20 | 20 | 59 | 3 | — |
| process 23 | 23 | 82 | 4 | — |
return total / count | — | 82 | 4 | 20.5 |
After the return, morning_mean refers to 20.5. The caller can store, compare,
or display that value:
print(f"Morning mean: {morning_mean:.1f} °C")
Q1. Trace an argument and return
What value is assigned to result?
result = mean([18, None, 22])
Use the mean definition above.
Compute it first, then check your number.
HintCount only available readings
The total is 40 and the count is 2.
SolutionThe call returns twenty
The loop processes 18 and 22, giving total = 40 and count = 2. The return
expression sends 20 back to result.
Printing Is Not Returning
This function displays a value but does not return that value:
If a function reaches the end of its body without an explicit return, Python
returns None:
The output is:
Mean: 20.5 °C
None
The first line is displayed inside show_mean. The second line displays the
value stored in shown. Printing is an action for a reader; returning provides
a value that the surrounding program can use.
Q2. Separate display from return
What value is assigned to stored?
Select one choice, then check.
HintLook for a return statement
The function body contains only print().
SolutionThe call returns None
show_count(4) displays a line and then reaches the end of its body. Python
returns None, so stored refers to None.
Define the No-Reading Result
The first mean definition assumes that at least one reading is available. An
empty list or a list containing only None leaves count at zero, so division
cannot produce a mean.
Handle that case before the ordinary return:
The first return is an early return. When no available reading exists, it
sends None to the caller and ends that call. Otherwise Python continues to
the ordinary mean calculation.
Now the same function has a clear result for several datasets:
None means that no numerical mean is available. It does not mean that the
mean is zero.
Q3. Repair the unavailable result
Repair mean so the program displays 20.0 and then None.
Editable Python
Ready to run.
HintPreserve the meaning of absence
Change the first return value. Zero is a numerical result; None records
that no numerical mean exists.
SolutionReturn None for no available readings
A parameter receives the caller's input, and return sends a result back.
mean now handles ordinary and unavailable data; next we will trace the
fresh local work created by each call.