
Fill in
Typed Quest Scrolls
JSON-style data often arrives as dicts with fixed keys. A
TypedDict lists those keys and the type of each value:
from typing import TypedDict
class Lantern(TypedDict):
island: str
fuel: int
lamp: Lantern = {"island": "Reef", "fuel": 3}
At runtime lamp is just a normal dict, the class only teaches
type checkers the shape, so they can flag lamp["fule"] typos.
Literal
Literal limits a value to a few exact options:
from typing import Literal
Direction = Literal["north", "south"] # a type alias
A checker will now reject "nrth". Way better than "any string"!
Assignment
Replace each ___:
- A
Questhas aname(str), areward(int) and astatusthat is theStatusliteral. completereturns a copy of the quest with status"done".open_rewardsadds up the rewards of quests whose status is"open".
Replace each ___:
- A
Questhas aname(str), areward(int) and astatusthat is theStatusliteral. completereturns a copy of the quest with status"done".open_rewardsadds up the rewards of quests whose status is"open".
main.py Python sleeping
Press Run to try your code, then Check to test it.