What Are Loops?

STEP 1 — What Are Loops? (a.k.a. Teaching Your Code to Stop Being Lazy)

Alright, young coder — welcome to the part of Python where your program finally learns how to do things over and over again without you writing the same line 47 times.

Because let’s be honest: No one wants to type “attack enemy” manually every turn. Your fingers would fall off.

Loops fix that.


1. What Is a Loop?

A loop is a command that tells your program:

“Hey, Repeat this thing… until I say stop.”

It’s like giving your code a superpower: infinite patience.

Games use loops for everything:

  • regenerating health
  • spawning enemies
  • checking inventory
  • running battle turns
  • counting down timers
  • making sure your character doesn’t fall through the floor

If something repeats in a game, a loop is behind it.


2. Why Loops Matter in Games

Imagine a game without loops:

  • Enemies attack once and retire
  • Health regenerates exactly zero times
  • Timers just… don’t time
  • Your character moves one step and gives up

Loops keep the game world alive, moving, and reacting.


3. The Two Loop Types

  • while loop → Repeats as long as something is True.
    Like:
    “Keep running until you hit a wall.”
  • for loop → Repeats for each item in a list.
    Like:
    “Check every item in the inventory.”
    You’re about to master both.

You’re about to master both.


Practice (Your Turn!)

Mini-Challenge

Challenge:

Write a loop that prints “Training…” three times.

Modify the code and click Run.

Boom. Your first loop.
Your code just did something three times without complaining.
Ready for Step 2, where we dive into the mighty while loop — the loop that keeps going like a toddler who refuses to sleep.