Capstone day. You want one function that takes a list of transactions and a title, and returns a printable report — title, separator line, category rows sorted alphabetically, and a grand total. What's the overall shape?
Collect lines in a list, then join them with \n at the very end. That way each line is appended independently and the newline logic lives in one place.
Exactly right. "\n".join(lines) is Python's canonical way to glue text lines together. It also keeps the function flexible — if you ever swap \n for \r\n or |, only the join line changes:
lines = [title, "-" * len(title)]Then you walk the dict of category totals and append one row per category, sorted alphabetically.
And sorted(by_cat.items()) — that gives me (category, total) pairs in order?
Yes, tuples. Python unpacks tuples in the for header — for cat, total in sorted(by_cat.items()): gives you both halves directly. Then an f-string formats each row, and "\n".join(lines) stitches the full report together at the end:
def build_report(txns, title):
lines = [title, "-" * len(title)]
by_cat = {}
for t in txns:
by_cat[t["category"]] = by_cat.get(t["category"], 0.0) + t["amount"]
for cat, total in sorted(by_cat.items()):
lines.append(f"{cat}: ${total:.2f}")
grand = sum(t["amount"] for t in txns)
lines.append(f"Total: ${grand:.2f}")
report = "\n".join(lines)
print(report)
return reportThe separator "-" * len(title) — is that real Python? Multiplying a string by an integer?
It is. "-" * 5 is "-----" — a five-dash string. Handy anywhere you want a divider, a spacer, or a ruler line under a heading.
Twenty-eight days and every pattern I've learned shows up in this one function. This is the tracker.
One function, every pattern. You've earned Day 28 — run it, change the title, watch the report render.
TL;DR: collect lines → append sorted category rows → append total → "\n".join.
"-" * len(title) — separator of matching widthsorted(dict.items()) — alphabetical key order"\n".join(lines) — canonical multi-line glue| Week | Pattern used here |
|---|---|
| 1 | f-string with :.2f |
| 2 | for loop, accumulator |
| 3 | dict accumulator, sorted() |
| 4 | composition — all of the above |
Ahead: a loop you can write in one line is called a comprehension — you'll see them next track.
Capstone day. You want one function that takes a list of transactions and a title, and returns a printable report — title, separator line, category rows sorted alphabetically, and a grand total. What's the overall shape?
Collect lines in a list, then join them with \n at the very end. That way each line is appended independently and the newline logic lives in one place.
Exactly right. "\n".join(lines) is Python's canonical way to glue text lines together. It also keeps the function flexible — if you ever swap \n for \r\n or |, only the join line changes:
lines = [title, "-" * len(title)]Then you walk the dict of category totals and append one row per category, sorted alphabetically.
And sorted(by_cat.items()) — that gives me (category, total) pairs in order?
Yes, tuples. Python unpacks tuples in the for header — for cat, total in sorted(by_cat.items()): gives you both halves directly. Then an f-string formats each row, and "\n".join(lines) stitches the full report together at the end:
def build_report(txns, title):
lines = [title, "-" * len(title)]
by_cat = {}
for t in txns:
by_cat[t["category"]] = by_cat.get(t["category"], 0.0) + t["amount"]
for cat, total in sorted(by_cat.items()):
lines.append(f"{cat}: ${total:.2f}")
grand = sum(t["amount"] for t in txns)
lines.append(f"Total: ${grand:.2f}")
report = "\n".join(lines)
print(report)
return reportThe separator "-" * len(title) — is that real Python? Multiplying a string by an integer?
It is. "-" * 5 is "-----" — a five-dash string. Handy anywhere you want a divider, a spacer, or a ruler line under a heading.
Twenty-eight days and every pattern I've learned shows up in this one function. This is the tracker.
One function, every pattern. You've earned Day 28 — run it, change the title, watch the report render.
TL;DR: collect lines → append sorted category rows → append total → "\n".join.
"-" * len(title) — separator of matching widthsorted(dict.items()) — alphabetical key order"\n".join(lines) — canonical multi-line glue| Week | Pattern used here |
|---|---|
| 1 | f-string with :.2f |
| 2 | for loop, accumulator |
| 3 | dict accumulator, sorted() |
| 4 | composition — all of the above |
Ahead: a loop you can write in one line is called a comprehension — you'll see them next track.
Write `build_report(txns, title)` that returns a multi-line string: title, dashes (same width), each category row sorted alphabetically with amount in dollars, then the grand total.
Tap each step for scaffolded hints.
No blank-editor panic.