Looping Through Dictionaries

STEP 3 — Looping Through Dictionaries

(a.k.a. Reading Game Stats Like You’re Inspecting a Boss Before Battle)

You’ve learned how to create dictionaries.
You’ve learned how to access and change values.

Now it’s time to loop through them, which is how real games display stats, menus, enemy info, and item details.

Let’s turn your dictionary into a readable, scrollable, game-ready info panel.


1. Looping Through KEYS

This loop gives you each key in the dictionary.

player = {
    "name": "Aria",
    "health": 100,
    "level": 3
}

for key in player:
    print(key)

This prints:
name
health
level

Perfect for listing stat categories.

Copy code from above and paste here.

2. Looping Through VALUES

Sometimes you only want the data, not the labels.

player = {
    "name": "Aria",
    "health": 100,
    "level": 3
}
for value in player.values():
 print(value)

This prints:
Aria
100
3

Great for quick displays or comparisons.

Paste code here.

3. Looping Through BOTH (key + value)

This is the most useful loop — the one games use to show full stat screens.

player = {
    "name": "Aria",
    "health": 100,
    "level": 3
}
for key, value in player.items():
 print(key, ":", value)

This prints:
name : Aria
health : 100
level : 3

Now your dictionary is basically a character sheet.

Paste code here.

4. Why This Matters for Games

Looping through dictionaries lets you build:

  • player stat menus
  • enemy info screens
  • item detail windows
  • shop item descriptions
  • settings menus
  • quest logs

Any time you need to display structured data, this is your tool.


Mini-Challenge #1

Challenge:
Create a dictionary called enemy with:

  • name
  • health
  • attack

Then loop through it and print each key and value.

Modify and Run

Mini-Challenge #2

Challenge:
Create a dictionary called item with

  • name
  • type
  • value

Then loop through just the values and print them.

Modify and Run