

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
You can also annotate the type that you expect a function to return. When you know what types go into and come out of a function, you can (probably) use it without having to read every line of the function body. Return types come after the parameter list, before the colon:
def add_gold(current_gold: int, found_gold: int) -> int:
return current_gold + found_gold
The -> int means this function is expected to return an integer.
The syntax is a bit different from type hints on variables and parameters: we use -> instead of :, and there's no variable name before the type hint. This is because it doesn't really matter what name (if any) the function uses internally for the return value; we just care about the type.
Here's another example:
def get_greeting(player_name: str) -> str:
return f"Welcome, {player_name}!"
The -> str means this function is expected to return a string.
Fantasy Quest uses item descriptions in shop menus. The get_item_description function already works, and its parameters already have type hints.
Add a str return type hint to get_item_description. Don't change the function body.