Python · Level 2 Project

Grade Book

Put the level together: read messy score lines, skip the broken ones safely, group scores by student and compute averages.

  • try / except to skip bad input
  • defaultdict(list) to group values
  • Dict comprehensions and round()

Real data is never clean. Parse each line inside try, skip the ones that fail, and collect the good values with a defaultdict.

from collections import defaultdict

totals = defaultdict(list)
for line in ["a=1", "broken", "a=3"]:
    try:
        key, value = line.split("=")
        number = int(value)     # parse first...
    except ValueError:
        continue                # ...so bad lines never create a key
    totals[key].append(number)
# {'a': [1, 3]}

Write average_scores(lines) for lines like "Ana: 90". Return a dict mapping each name to the average of its scores, rounded to 1 decimal place, in order of first appearance. Skip lines that can't be parsed.

solution.py
Test Suites Pending
Stdout