Dataclasses, enumerations and types · 9 / 15 Exercise Medium

Grades with default values

Objective

Create a Python program with a dataclass that has a list as a default value, using field(default_factory=list).

Instructions

  • Define @dataclass class Student with name: str and grades: list[int] = field(default_factory=list), and a method average(self) that returns the average of the grades, or 0 if there are none.
  • Read an integer n and then n lines, each with a name followed by zero or more integer grades. Create each student with only the name and then add the grades with student.grades.append(...).
  • Show one line per student: name: average, with the average to 2 decimals.
Input
3
Ana 7 9
Luis
Eva 5 6 10
Output
Ana: 8.00
Luis: 0.00
Eva: 7.00

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise