Dictionaries (dict)
In a phone book you look up a number by name, not by row number. Python has a dictionary for exactly this: each key maps to a value.
How a dictionary is made
Section titled “How a dictionary is made”Curly braces { } plus key: value pairs:
Inputs (for input())
One value per line — read in order by input().
player["name"] — access by key, not by index: “the value of the name key in the player dictionary.”
Change a value, add a new one
Section titled “Change a value, add a new one”Inputs (for input())
One value per line — read in order by input().
Assigning to an existing key updates its value; assigning to a new key adds a pair. This is exactly how character data is stored in video games. 🎮
Missions
Section titled “Missions”Player card
+10 XP ✓ CompletedBuild a player dictionary (with name and score keys) and print the info like Aysel — 120 points. Tip: inside an f-string, write the dictionary key with single quotes: {player['name']}.
Inputs (for input())
One value per line — read in order by input().
Score boost
+15 XP ✓ CompletedIncrease the player’s score by 50 (building on the old value!) and print the new score.
Inputs (for input())
One value per line — read in order by input().
Phone book
+15 XP ✓ CompletedFind and print the number of the person the user asked about, from the dictionary. (The inputs include Aysel.)
Inputs (for input())
One value per line — read in order by input().
🏅 You finished this module! Lists and dictionaries — a program’s memory store. In the next module we tidy up the code: you’ll write your own functions. Continue →