Lookup
Now that Hoppy has the spellbook, he needs to find the right spell for the situation.
You can look up a value by using its key inside square brackets [].
Casting Spells
spells = {
"fire": "🔥",
"water": "💧"
}
# Look up the "fire" spell
effect = spells["fire"]
print(effect) # Output: 🔥
Your Mission
1
The Library
We have a dictionary library with book locations.
2
Find It
Find the location of the "Map" and save it in a variable called location.
3
Go There
Print the location.
Suggested SolutionClick to expandClick to collapse
It's just like using an index in a list, but you use the key name instead of a number.
# library is predefined location = library["Map"] print(location)
Advanced TipsWant more? Click to expandClick to collapse
If you ask for a key that doesn't exist, Python will give you a KeyError. You can use .get() to be safe!
print(spells.get("wind")) # Output: Nonepymain.py
Loading...
Terminal
Terminal
Ready to run...