Dataclasses, enumerations and types · 14 / 15 Exercise Medium

Inventory by category

Objective

Create a Python program that combines an enumeration and a dataclass to group items by category.

Instructions

  • Define class Category(Enum) with FOOD = 1, TOOLS = 2 and TOYS = 3, and @dataclass class Item with name: str, price: int and category: Category.
  • Read an integer n and then n lines with the format name price code, where code is the value of the category (converted with Category(int(code))).
  • For each category, in the order of the enumeration, show NAME: T, with the sum of the prices of its items (0 if it has none).
  • At the end show Total: X, with the sum of all the prices.
Input
4
pan 2 1
martillo 15 2
leche 1 1
coche 20 3
Output
FOOD: 3
TOOLS: 15
TOYS: 20
Total: 38

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

Start exercise