Combining Loops, Lists, and Logic
Ohhhh yes โ Step 4 is where everything in Unit 4 comes together and your code becomes a full-blown game engine machine.
This is the moment loops stop being โcute little repeatsโ and start powering enemy waves, battle turns, inventory checks, and all the chaos teens love.
Letโs finish this unit with style.
Welcome to the grand finale of Unit 4.
Youโve met:
- the while loop โ chaotic energy, runs forever if youโre not careful
- the for loop โ organized, responsible, probably drinks herbal tea
- lists โ your gameโs backpack, enemy roster, and snack storage
Now we smash them together into actual gameplay systems.
This is where your code starts acting like a real game.
1. Enemy Wave Generator (Yes, Like a Mini Boss Fight)
enemies = ["goblin", "orc", "slime"]
for enemy in enemies:
print("A wild", enemy, "appears!")
print("Prepare for battle!")
Your game now introduces enemies one by one like itโs hosting a fantasy talent show.
2. Battle Turn Loop (The Classic RPG Moment)
health = 30
while health > 0:
print("Enemy attacks!")
health = health - 10
print("Your health:", health)
This loop keeps running until the player is basically like,
โOkay, I get it, Iโm dying.โ
3. Inventory Check + Loop Combo
inventory = ["potion", "sword", "map"]
for item in inventory:
if item == "potion":
print("You drink a potion and heal!")
Your game now knows how to look through a list and react to specific items.
This is how real inventory systems work.
4. Loop Control: break and continue
Sometimes you need to stop a loop early.
Sometimes you need to skip something.
Pythonโs got you.
break โ yeets you out of the loop
for enemy in enemies:
if enemy == "orc":
print("Too strong! Retreat!")
break
continue โ skips one loop and keeps going
for item in inventory:
if item == "map":
continue
print("Using:", item)
Your game now has loop strategy.
Practice (Your Turn!)
Mini Challenge
You have:inventory = ["potion", "key", "torch"]
- prints “Found the key!” when it sees “key”
- stops the loop immediately after finding it
Write a loop that:
Boom.
Your game now searches like a detective with a deadline.
You Did It
Youโve officially mastered loops.
Your code can now repeat, search, automate, and run gameplay systems like a real game engine.


