
Boss
Boss: Guild Inventory

Boss Fight!
Every passing test breaks a shield.
The Lantern Guild's vault is chaos: pearls in the boot cupboard, three ledgers that disagree. The quartermaster hands you the keys: "Build me an inventory system I can trust."
Rarity is ready. You'll build the rest in four steps. Run your
code after each step: the tests show which parts already work.
Assignment
Step 1: the items
- Make
Itema frozen dataclass with the fieldsname: str,rarity: Rarityandvalue: int.
Step 2: the stock (like the Shelf from the last lesson)
Inventory(owner)storesowner. It keeps a dict that maps eachItemto how many you have.count(item)returns how many you have, or0.add(item, qty=1)raisesValueErrorifqtyis not positive.remove(item, qty=1)raisesValueErrorif you don't have that many. Items that reach 0 disappear completely.
Step 3: reports
- A read-only property
total_value: for every item, multiply itsvalueby its count, and add them all up. names(rarity)returns the names of the items you hold with that rarity, sorted A to Z.__str__returns text likeAda's vault: 3 items worth 90 gold. "Items" means the total count of all units.
Step 4: loading from text
- A class method
from_lines(owner, lines)builds a full inventory. Each line looks like"Moon Pearl,RARE,40,2": the name, the rarity's name, the value and the quantity.
Step 1: the items
- Make
Itema frozen dataclass with the fieldsname: str,rarity: Rarityandvalue: int.
Step 2: the stock (like the Shelf from the last lesson)
Inventory(owner)storesowner. It keeps a dict that maps eachItemto how many you have.count(item)returns how many you have, or0.add(item, qty=1)raisesValueErrorifqtyis not positive.remove(item, qty=1)raisesValueErrorif you don't have that many. Items that reach 0 disappear completely.
Step 3: reports
- A read-only property
total_value: for every item, multiply itsvalueby its count, and add them all up. names(rarity)returns the names of the items you hold with that rarity, sorted A to Z.__str__returns text likeAda's vault: 3 items worth 90 gold. "Items" means the total count of all units.
Step 4: loading from text
- A class method
from_lines(owner, lines)builds a full inventory. Each line looks like"Moon Pearl,RARE,40,2": the name, the rarity's name, the value and the quantity.
main.py Python sleeping
Press Run to try your code, then Check to test it.