The Summary Spell
Deeper inside the craft bench, Hoppy starts wanting more than one number or one pair of results. Sometimes it wants a whole little report card back from a function: how many marks appeared in total, how many different marks showed up, and what the latest mark was.
This lesson practices one more step forward: a function can return a more structured result. Today we will use a small dict so one function call can hand back one tidy summary for later use.
Collect several related facts into one small report
If the caller will care about several named pieces of information afterward, a dict often makes the result much clearer. Instead of receiving loose values, you get a tiny report that you can read by key.
mark_log = ["fern", "fern", "moon"]
def build_summary_report(mark_log):
total_marks = len(mark_log)
unique_marks = len(set(mark_log))
latest_mark = mark_log[-1]
return {
"total_marks": total_marks,
"unique_marks": unique_marks,
"latest_mark": latest_mark,
}
summary_report = build_summary_report(mark_log)
unique_mark_count = summary_report["unique_marks"]
watch_label = "Watch next: " + summary_report["latest_mark"]
print("Summary report:", summary_report)
print("Unique mark count:", unique_mark_count)
print("Watch label:", watch_label)
Inside summary_report, three related facts are stored in one named structure. That means the outside code can inspect the whole report or pull out one specific piece such as "unique_marks" or "latest_mark" for the next step.
A structured return value is meant to help the next steps
The main idea today is this: if a function finishes a task and needs to hand back several related facts whose names matter, returning a small dictionary can be cleaner than scattering loose values around. You do not need to think of this as complex data modeling. It is enough to see it as one little report card with labeled fields.
The starter already calculates total_marks, unique_marks, and latest_mark. Put them into a dictionary and return that dictionary from the function.
Use summary_report = build_summary_report(mark_log) to keep the whole report, then read summary_report["unique_marks"] and summary_report["latest_mark"] for the later lines.
When you run the code, you will first see the full report, then you will see follow-up lines built from keys inside that report. The point here is not memorizing dict definitions. It is seeing that a function can return one structured result that later code can keep using.
If one small tool needs to hand back several related facts and those facts are easier to understand with names, a dict is very handy. The caller can keep the whole report and also read one key at a time.
Suggested SolutionExpandCollapse
mark_log = ["fern", "moon", "fern", "spark", "moon", "fern"]
def build_summary_report(mark_log):
total_marks = len(mark_log)
unique_marks = len(set(mark_log))
latest_mark = mark_log[-1]
return {
"total_marks": total_marks,
"unique_marks": unique_marks,
"latest_mark": latest_mark,
}
summary_report = build_summary_report(mark_log)
unique_mark_count = summary_report["unique_marks"]
watch_label = "Watch next: " + summary_report["latest_mark"]
print("Summary report:", summary_report)
print("Unique mark count:", unique_mark_count)
print("Watch label:", watch_label)Advanced TipsWant more? Click to expandClick to collapse
This lesson does not expand into complex nested structures, and it does not mix returned data with file saving. Today it only shows one thing clearly: a function can hand back one tidy report, and that report can keep serving the next lines of code.
Carrying that feeling forward will help a lot when you later meet JSON, CSV, and more realistic data outputs.