Combining Loops, Lists, and Logic

STEP 4 โ€” Combining Loops, Lists, and Logic (a.k.a. Your Gameโ€™s Hamster Wheel of Power)

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.

Click Run

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.โ€

Click Run

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.

Click Run

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"]

    Write a loop that:

  • prints “Found the key!” when it sees “key”
  • stops the loop immediately after finding it
Modify and Run

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.