← Back to Loops

Inventory Lister

4 of 6

Exercise 3: Inventory Lister

You have a list of items in your game inventory. Use a for loop to print each item with its position number.

Given inventory:

inventory = ["sword", "shield", "potion", "bow", "helmet"]

Expected output:

1. sword
2. shield
3. potion
4. bow
5. helmet

You have 5 items.

Starter Code

inventory = ["sword", "shield", "potion", "bow", "helmet"] for i in range(___): print(f"{}. {inventory[___]}") print(f"\nYou have {___} items.")

Hints

Hint 1 — How many times to loop?

Loop through all items: range(len(inventory)) gives you indices 0, 1, 2, 3, 4.

Hint 2 — Numbering starts at 1, not 0

The display number is i + 1 (since i starts at 0), but the list index is just i.

print(f"{i + 1}. {inventory[i]}")
Hint 3 — Total count

Use len(inventory) to get the number of items.

Try it yourself

Code: Inventory Lister

Loading Python runtime…
Python
Loading...
bash
$ Click "Run" to execute your code...