Final project: a study log
Put it all together: parse messy lines, total them with a dictionary, and print a clean report.
25 min
You will build the core of a study log. Each entry is a line like "python:25": a subject and the minutes studied. Over a week you have many lines, and you want a report of the total minutes for each subject, biggest first.
This uses everything from the course: strings (split, strip), loops, a dictionary for totals, a list you sort, functions that return values, and a little error handling.
Break the job into three small functions. Each does one thing and can be tested alone:
- 1.
parse_entry(line)turns"python:25"into["python", 25]. - 2.
totals(lines)adds up the minutes per subject into a dictionary. - 3.
report(lines)returns the final text, one line per subject.
Build them one at a time. Write the smallest version that passes its tests, then move on.
Each function feeds the next.
Write all three functions.
parse_entry(line): split on:; return a list[subject, minutes]with the subject lowercase and stripped, and minutes as an int." Python : 25 "becomes["python", 25].totals(lines): a dictionary of subject to total minutes. Skip any line that has no:or whose minutes are not a whole number.report(lines): a string with one line per subject in the formpython: 45 min, biggest total first, ties by name A to Z, lines joined with\n. For no valid lines return"No entries".
Your code must satisfy
- parse_entry(" Python : 25 ")→["python",25]parse
- totals(["python:25", "math:30", "python:20"])→{"python":45,"math":30}totals add up
- totals(["python:25", "oops", "math:ten", "math:5"])→{"python":25,"math":5}totals skip bad lines
- report(["math:30", "python:25", "python:20"])→"python: 45 min\nmath: 30 min"report order
- report(["b:10", "a:10"])→"a: 10 min\nb: 10 min"report ties
- report([])→"No entries"report empty
def parse_entry(line):
pass
def totals(lines):
pass
def report(lines):
passMake it yours
Extend the study log with one idea of your own, and run it with your real week of study. Some options: a daily goal that prints Goal reached or how many minutes are left; the longest single session; the average session per subject. Keep every function small, give each a clear name, and test it with at least three inputs, including an empty one.
Deliverables
- 01Your extended code with the three original functions still passing
- 02A short note: what you added, and the one input that surprised you
Describe your extension and paste the code for it.
What you can now do
Next, pick a project you care about and build it: small, real, finished beats big and abandoned.
- Split a job into small functions that each do one thing and can be tested alone.
- Parse messy input at the edge, then work with clean values.
- A dictionary accumulates totals; sorting its items turns them into a report.
- Decide what to do with bad input (skip, default, stop) and write it down in the function.
Last checks.
Which structure is best for "total minutes per subject"?
- AA string
- BA dictionary
- CA single int
- DA bool
sorted(d.items(), key=lambda p: p[1], reverse=True) sorts by...
- Akey, A to Z
- Bvalue, biggest first
- Cvalue, smallest first
- Dlength of the key
Why is try / except ValueError used around the parsing?
- ATo make the code run faster
- BTo skip bad lines instead of crashing
- CTo hide every bug
- DTo convert text to a list
What is len({"a": 1, "b": 2, "a": 3})? (Keys are unique.)
What does "x:5:7".split(":") return?
- A`["x", "5:7"]`
- B`["x", "5", "7"]`
- C`"x 5 7"`
- D`["x:5:7"]`
Practise this lesson, not just read it
In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of Python for Complete Beginners is planned around your week. Free.