8. Loops - Repetition Magic!
Learn how to make your code repeat tasks automatically like a true coding wizard!
Level Up with Loop Power! 🔄
Hey coding champions! Remember how we made decisions with conditionals in our last adventure? Now it's time to learn something incredible - LOOPS! Just like in games where you collect coins or defeat multiple enemies, loops help you repeat actions without writing the same code over and over! 🎮
Today's Quest! 🎯
You'll master the power to:
- Make your code repeat actions automatically! 🔄
- Process entire inventories with just a few lines of code! 🎒
- Create enemy waves in your game! 👾
- Level up through multiple rounds! ⬆️
The "for" Loop: Your First Repeat Spell! 🪄
# Remember our inventory from conditionals?
inventory = ["Sword", "Shield", "Potion", "Map"]
# Let's check each item!
for item in inventory:
print(f"Found item: {item} ✨")
# We can use our conditional powers here too!
if item == "Sword":
print("Ready for battle! ⚔️")
The Range Power: Controlling Your Loops! 🎯
# Count up your power levels!
print("Powering up! 🔋")
for level in range(1, 6): # Goes from 1 to 5
print(f"Power Level {level}!")
# Skip counting (like taking every 2nd treasure)
print("\nCollecting treasures! 💎")
for treasure in range(0, 10, 2): # Steps of 2
print(f"Found treasure #{treasure}!")
The "while" Loop: Repeat Until Conditions Change! 🔄
player_health = 100
healing_potion = 20
# Keep healing until full health!
while player_health < 100:
player_health += healing_potion
print(f"Healing... Current health: {player_health} ❤️")
# Fight enemies until victory!
enemy_count = 5
while enemy_count > 0:
print(f"Defeating enemy! {enemy_count} remaining! 👾")
enemy_count -= 1
Special Powers: break and continue! ⚡
# Break: Escape from the dungeon early!
print("Exploring the dungeon... 🏰")
for room in range(1, 6):
print(f"Checking room {room}")
if room == 3:
print("Found the treasure! Let's get out! 💎")
break # Exit the loop early!
# Continue: Skip trapped rooms!
print("\nExploring more rooms... 🏰")
for room in range(1, 6):
if room == 2:
print(f"Room {room} is trapped! Skip it! ⚠️")
continue # Skip this room!
print(f"Room {room} is safe! ✨")
Loops with else: Victory Conditions! 🏆
# Search for the magic sword
sword_locations = ["cave", "castle", "forest", "mountain"]
magic_sword_found = False
for location in sword_locations:
print(f"Searching in {location}...")
if location == "castle":
print("Found the magic sword! ⚔️✨")
magic_sword_found = True
break
else: # This runs if we don't break the loop
print("The sword wasn't in any location! 😮")
🎮 Mini-Game: The Treasure Hunter!
# You have a map of treasure values!
treasures = [100, 50, 20, 200, 10, 150]
# Your mission:
# 1. Calculate total treasure value
# 2. Find the most valuable treasure
# 3. Count treasures worth more than 100
total_value = 0
best_treasure = 0
rich_treasures = 0
# Your code here!
🌟 Super Challenge: The Dragon's Lair!
dragon_path = [
"safe", "safe", "trap", "safe", "dragon", "safe", "trap"
]
# Create a loop that:
# 1. Moves through the path
# 2. Avoids traps (use continue)
# 3. Stops if dragon found (use break)
# 4. Counts safe rooms visited
# Your code here!
🎯 Debug Mission: Fix the Game Loop!
Can you spot what's wrong with these loops?
# Bug 1: Infinite Power-Up!
power = 1
while power < 10:
print(f"Power level: {power}")
# Oops! We forgot something!
# Bug 2: Skipping Treasures?
treasures = [100, 200, 300]
for i in range(1, len(treasures)):
print(f"Found treasure: {treasures[i]}")
# Bug 3: Wrong Battle Count
enemies = 5
while enemies >= 0:
print(f"Fighting enemy #{enemies}")
enemies -= 1
Final Boss Challenge: Create a Turn-Based Battle System!
# Create a battle system where:
player_hp = 100
dragon_hp = 150
turns = 1
# 1. Battle continues while both have HP
# 2. Each turn:
# - Player deals 20-25 damage
# - Dragon deals 15-20 damage
# 3. Print status each turn
# 4. Declare winner when one falls
# Your code here!
Remember:
for
loops are great for known number of repeatswhile
loops are perfect for unknown repeatsbreak
helps you escape loops earlycontinue
lets you skip to the next iteration- Combine loops with conditionals for awesome game logic!
Ready to become a Loop Master? Let's code! 🚀
Practice Quest: The Number Seeker!
Let's use our new loop powers to solve this quest:
numbers = [
951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941
]
print("Starting the number quest! 🔢")
for number in numbers:
# Stop if we find number 237
if number == 237:
print("Found 237! Quest complete! 🏆")
break
# Skip odd numbers using continue
if number % 2 != 0:
continue
print(f"Found even number: {number} ✨")
Ready to loop your way to victory? Let's start coding! 🚀