

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 2
click for more info
Not enough gems
Cost: 6 gems
1: Type Hints
incomplete
2: Basic Types
incomplete
3: Function Parameters
incomplete
4: Return Types
incomplete
5: Fixing Type Hints
incomplete
6: List and Set Hints
incomplete
7: Dictionary Hints
incomplete
8: Tuple Hints
incomplete
9: Specific Container Types
incomplete
10: Nested Types
incomplete
11: Optional Values
incomplete
12: Fix Code With Type Hints
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Sometimes we work with variables that may or may not have an "actual value." For example, a character might have a damage bonus, or they might not. If they don't, we can represent that lack of value with None.
The | operator indicates that a value can be of multiple types:
damage_bonus: int | None
That means damage_bonus can be either an integer (the bonus amount) or None. For another example, a function might return a prepared spell if one is ready, or None if no spell is prepared:
def get_prepared_spell(has_spell: bool) -> str | None:
if has_spell:
return "Fireball"
return None
Fantasy Quest characters that have their own mounts can summon them, from up to a limited distance. If the character doesn't have a mount, or if the mount is too far away to be summoned, the function returns None. Add type hints to the summon_mount function.
Don't change the function body.