We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Generators, Iterators and Streaming Data step 2 of 9
Pair Names with Scores and Reject a Length Mismatch
Your program reads two columns from a file, names and scores, and pairs them by
position. If one column is longer, a row is missing somewhere. The pairing must
then fail with a clear error, instead of dropping data the way plain zip
does.
align(["ann", "bob"], [91.0, 88.5])
# {"ann": 91.0, "bob": 88.5}
align(["ann", "bob", "cy"], [91.0, 88.5])
# raises LengthMismatch("length mismatch: names=3, scores=2")
What to write
Fill in align(names, scores). The starter gives you LengthMismatch, a
subclass of ValueError.
- Pair each name with the score at the same position, and return a dictionary from name to score.
- Use
zip(names, scores, strict=True), and run it to the end inside atryblock, for example withlist(). Strict zip raises only when the loop reaches the end of the shorter column. - Put only the zip inside the
try, so the onlyValueErrorit can catch is the length mismatch. - Catch that
ValueErrorand writeraise LengthMismatch(...) from exc, so zip's error stays attached as the cause. - The message is exactly
length mismatch: names=<n>, scores=<m>, where<n>islen(names)and<m>islen(scores). Use the same message whichever column is longer. - If a name appears more than once, the later score wins.
dict()does this for you.
Edge cases
- Two empty lists give
{}, with no error. - An empty list paired with a nonempty one is a mismatch, for example
length mismatch: names=0, scores=2. names=["a", "a"]withscores=[1.0, 2.0]gives{"a": 2.0}.
What the driver reports
Keep solve as it is. It calls align and returns
{"mapping": <your dictionary>, "error": ""}. When align raises
LengthMismatch, solve returns {"mapping": {}, "error": str(exc)}, so a
failed call never reports a partial mapping.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
Python reference solution
Sign in to track your attempts toward unlocking the reference solution.