🎯 What you will learn
- A dictionary pairs keys with values
- Look up a value using its key
- Check requirements before crafting
👋 Let’s get started!
In Minecraft, a recipe says what you need to craft a tool. In Python, a dictionary stores a recipe: each item name is a key, and the amount is its value. 🪓
🪜 Step by step
1. 📖 A dictionary
A dictionary uses {key: value} pairs.
recipe = {"wood": 2, "stone": 3}
print(recipe)
2. 🔍 Look up
Use the key in square brackets to find its value.
print(recipe["stone"])
3. ✅ Check first
Compare what you have with what the recipe needs.
have = 5
if have >= recipe["wood"]:
print("Enough wood!")
🧪 Try it!
Change the code and press ▶ Run to see what happens. You can’t break anything!
Craft a pickaxe
recipe = {"wood": 2, "stone": 3}
bag = {"wood": 5, "stone": 2}
for item in recipe:
if bag[item] >= recipe[item]:
print("Enough", item)
else:
print("Need more", item)🎨 Unplugged activity
🪓 Write a recipe for a sandwich as a dictionary (bread: 2, peanut butter: 1, jelly: 1). Then check whether your kitchen has enough!
Fix the recipe check
recipe = {"wood": 2}
bag = {"wood": 1}
if bag["wood"] >= recipe["wood"]:
print("Craft!")
else:
print("Get more wood")📖 Key words
Tap a card to flip it over!
- Dictionary
- Pairs of keys and values 📖
- Key
- The name you look up 🔑
- Value
- What the key stores 💎
🏆 Challenge
The recipe needs 4 sticks. You have 3. Print “Need more sticks”. Press ▶ Run.
Challenge
recipe = {"sticks": 4}
bag = {"sticks": 3}
Knowledge Check Quiz
- What is in {"wood": 2}?
- How do you find the stone amount?
- When should you craft?