Exercises

Trace definitions and calls, repair return flow and no-data behavior, choose clear arguments, write an independent test, and document one stable function contract.

Define, Call, and Trace

Q1. Distinguish a definition from a call

Which line runs the function body?

Choose one

Select one choice, then check.

HintLook for call parentheses

The function runs when Python reaches its name followed by () outside the definition.

SolutionThe call runs the body

show_summary() is the call. The earlier def statement records what that call should do.

Not attempted
Review

Not marked done.

Q2. Predict call order

What is the output order?

Choose one

Select one choice, then check.

HintPause at the call

Run the body where report() appears, then continue with the next line.

SolutionThe call appears between the two prints

Python displays before, enters the function and displays summary, then resumes and displays after.

Not attempted
Review

Not marked done.

Q3. Trace argument, local work, and return

Trace the call. Enter the argument length, final local count, and returned result in that order, separated by commas.

Compute it first, then check your number.

HintTrace the three values

The middle value is skipped by the condition.

SolutionThe function returns 2

The local count reaches 2. return count sends that value to the caller, so result becomes 2.

Not attempted
Review

Not marked done.

Return and Call Clearly

Q4. Replace printing with returning

Repair the function so the caller receives 20.0 and displays result: 20.0.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintChange the function’s effect

Replace print(...) inside the function with return ....

SolutionReturn the midpoint
Not attempted
Review

Not marked done.

Q5. Choose a readable call

Given format_mean(value, unit="°C"), which call clearly replaces the unit?

Choose one

Select one choice, then check.

HintFollow both roles

The function needs a value; the optional unit can be named.

SolutionName the optional choice

format_mean(68.9, unit="°F") is valid and makes the meaning of the second value visible.

Not attempted
Review

Not marked done.

Q6. Complete one useful default

Complete the definition so the first call uses °C and the second call can replace it with °F.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintPut the common choice in the definition

Change the second parameter to unit="°C".

SolutionSupply the common unit
Not attempted
Review

Not marked done.

Check and Document the Contract

Q7. Write a hand-reasoned test

Add one assertion that checks the mean of 6 and 10 against an independently calculated answer. The program should reach the final print.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintAdd, count, divide

(6 + 10) / 2 gives the expected result.

SolutionCheck against 8
assert mean([6, 10]) == 8
Not attempted
Review

Not marked done.

Q8. Repair the no-data result

Complete mean so empty and all-unavailable inputs return None without a division error.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintGuard the division

Before dividing, add if count == 0: return None.

SolutionReturn None before dividing
Not attempted
Review

Not marked done.

Q9. Assemble the tested interface

Complete the two docstrings and the four checks. The program should test ordinary, one-reading, empty, and all-unavailable inputs, then format results from two datasets.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintComplete the interface before running it

Use the two contract sentences from Lesson 6, then add the four assertions from Lesson 5.

SolutionDocument and check the complete program

Add """Return the mean of available readings, or None when none are available.""" to mean and """Return a labelled mean, or 'Mean unavailable' when value is None.""" to format_mean. Then add the four assertions shown in Lesson 5 before the final calls.

Not attempted
Review

Not marked done.

Pause and reflect

Which exercises were difficult, what mistake pattern did you notice, and what should you practice again? The note stays with this exercise set.

0 of 9 exercises marked done

Review

Not marked done.