Records You Can Trust

Python for AIHard+10 XP+10

Records You Can Trust

A teacher's scores arrive as text records like "ana:90". Some are broken.

Write two functions:

  1. parse_record(record) returns a tuple (name, score), with the name stripped of spaces and the score as a whole number. It raises a ValueError when:

    • there's no ":" in the record: raise ValueError("missing score: " + record)
    • the score can't be read by int(): let int() raise its own ValueError
    • the score is below 0 or above 100: raise ValueError("score out of range: " + record)

    A record has at most one colon. Strip spaces from the score text before int().

  2. load_records(records) runs parse_record on each record in order and returns a list of two things: a dictionary of name → score for the good records, and a list of the error messages (as text) for the bad ones. If a name appears twice, the later score wins.

Examples

records = ["ana:90", "ben", "cy:120", "dee: 75"]
returns [{'ana': 90, 'dee': 75}, ['missing score: ben', 'score out of range: cy:120']]
record = " ana : 88 "
returns ['ana', 88]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run