
Checkpoint
Checkpoint: The Cargo Manifest
The Guild airship is loading crates for Skyreach Isle. The quartermaster wants a typed cargo manifest, so that editors and type checkers can catch mistakes before take-off.
This checkpoint mixes the tools from this chapter: a TypedDict,
container types like list[str], X | None for "maybe a value", and a
generic function with a type parameter.
Assignment
- Make
CrateaTypedDictwith three keys:label(str),weight(int) andfragile(bool). heaviest(crates: list[Crate]) -> Crate | Nonereturns the crate with the biggest weight, orNoneif the list is empty.fragile_labels(crates: list[Crate]) -> list[str]returns the labels of the fragile crates, in order.pick[T](items: list[T], index: int) -> T | Nonereturnsitems[index], orNoneifindexis out of range. Use the new bracket syntax forT. (Don't worry about negative indexes.)
- Make
CrateaTypedDictwith three keys:label(str),weight(int) andfragile(bool). heaviest(crates: list[Crate]) -> Crate | Nonereturns the crate with the biggest weight, orNoneif the list is empty.fragile_labels(crates: list[Crate]) -> list[str]returns the labels of the fragile crates, in order.pick[T](items: list[T], index: int) -> T | Nonereturnsitems[index], orNoneifindexis out of range. Use the new bracket syntax forT. (Don't worry about negative indexes.)
main.py Python sleeping
Press Run to try your code, then Check to test it.