Back to Minecraft Lessons
Lesson 2 · Self-Paced

Loops, Repetition, and Agent Patterns

Make the Agent repeat actions automatically with for loops.

ESSENTIAL QUESTION

Why do programmers use loops instead of writing the same instruction over and over?

By the end of this lesson, I will...

  • Explain why loops make programs shorter and easier to change.
  • Use a for loop to repeat Agent movement.
  • Use a loop to make the Agent place or destroy blocks repeatedly.
  • Predict how many times a repeated action will happen.
  • Modify loop values to change the size of a path, wall, or pattern.
  • Debug a loop when the Agent repeats too many or too few times.
  • Create an original Agent build that uses repetition.
Every Lesson

Start Here First — Every Single Time

Every Minecraft lesson begins with a brand-new world. Do not skip this. Open Lesson 0, follow the setup steps, then come back here and continue with Step 1.

Open Lesson 0 — World Setup

STEP 1 Warm-up: The Case for Loops

Imagine you wanted to walk down a hallway and lay down a tile every step. Imagine the hallway is 20 tiles long. Write out (on paper or just in your head) the instructions you would give the Agent.

Think about it first — what is annoying about those instructions?

You probably wrote move, place, move, place... 20 times in a row. That is 20 identical pairs of lines — 40 lines total. If you decide tomorrow that the hallway should be 30 tiles long, you have to add 10 more pairs. If it should be 100 tiles long, good luck.

The big idea: Repeating yourself in code is slow to write, hard to change, and easy to mess up. A loop lets you say "do this thing 20 times" in one line. Change the 20 to 100 and you're done.

Today's Vocabulary

LoopA programming structure that repeats instructions.
IterationOne repetition of a loop.
For LoopA loop that repeats a specific number of times.
Nested LoopA loop inside another loop.
EfficiencyWriting code without unnecessary repetition.

STEP 2 Open MakeCode (Python Only)

You should already be in your fresh flat world from Lesson 0. If not, go back and complete Lesson 0 first.

  1. In the Minecraft world, press C on your keyboard to open the Code Builder.
  2. In the Code Builder window, choose MakeCode.
  3. Click New Project and give it a name — Lesson 2 - Loops works.
  4. Click Code options, then select Python Only.
  5. Click Create. The project will open straight into the Python editor.

STEP 3 The Same Program, Two Ways

Here is a program that makes the Agent walk forward 5 blocks, placing stone underneath each step — written the repetitive way and the loop way. Look at both. They do the exact same thing.

Without a loop — tedious

def on_chat():
    agent.set_item(STONE, 64, 1)
    agent.move(FORWARD, 1)
    agent.destroy(DOWN)
    agent.place(DOWN)
    agent.move(FORWARD, 1)
    agent.destroy(DOWN)
    agent.place(DOWN)
    agent.move(FORWARD, 1)
    agent.destroy(DOWN)
    agent.place(DOWN)
    agent.move(FORWARD, 1)
    agent.destroy(DOWN)
    agent.place(DOWN)
    agent.move(FORWARD, 1)
    agent.destroy(DOWN)
    agent.place(DOWN)
player.on_chat("long", on_chat)

With a loop — clean

def on_chat():
    agent.set_item(STONE, 64, 1)
    for i in range(5):
        agent.move(FORWARD, 1)
        agent.destroy(DOWN)
        agent.place(DOWN)
player.on_chat("short", on_chat)
Read the loop in English: for i in range(5): means "do the indented stuff below me 5 times." The variable i is a counter that goes 0, 1, 2, 3, 4 — one value per iteration. You don't have to use i for anything yet, but it has to be there.

STEP 4 Run Your First Loop

Delete anything in your MakeCode editor and paste the loop version below. Save and try it.

def on_chat():
    agent.set_item(STONE, 64, 1)
    for i in range(5):
        agent.move(FORWARD, 1)
        agent.destroy(DOWN)
        agent.place(DOWN)
player.on_chat("path", on_chat)

What this program says

  • agent.set_item(STONE, 64, 1) — loads 64 stone (a full stack) in inventory slot 1, so the Agent never runs out.
  • for i in range(5): — repeat the indented block 5 times.
  • agent.move(FORWARD, 1) — move forward one block.
  • agent.destroy(DOWN) — clear the ground under the Agent.
  • agent.place(DOWN) — drop one stone in that empty spot.
Predict before you run — how many stone blocks will appear in the world?

The loop runs 5 times, and each iteration places exactly one stone — so you'll see a line of 5 stone blocks behind the Agent.

Run it

  1. Switch to Minecraft.
  2. Open chat (T), type path, press Enter.
  3. Count the stone blocks. Did you get 5?

STEP 5 Change the Number, Change the Result

This is the whole point of loops: you change one number to change the size of the result. Try at least two of the modifications below. Run after each change so you can see the difference.

Modifications

  1. Change range(5) to range(15). Run the program. Did you get 15 blocks?
  2. Change range(15) back to a smaller number, like range(3).
  3. Change agent.move(FORWARD, 1) to agent.move(FORWARD, 2). What happens to the spacing of the stone blocks? (Hint: this is a common bug.)
  4. Add an agent.turn(RIGHT_TURN) call after the place line but still inside the loop. What pattern does that create?
One thing at a time: Change one value, save, run, look at the result, then change the next thing. If your Agent does something unexpected, undo your last change and try again.

STEP 6 Debugging: Indentation Matters

Here is a program a student wrote. They wanted the Agent to walk 10 blocks and leave a stone trail behind every step. But when they ran it, only one stone block appeared at the end of the path. Read the code carefully.

def on_chat():
    agent.set_item(STONE, 64, 1)
    for i in range(10):
        agent.move(FORWARD, 1)
    agent.destroy(DOWN)
    agent.place(DOWN)
player.on_chat("oops", on_chat)
Predict: why does only one block appear instead of 10?

The destroy and place lines are not indented the same as agent.move. That means they are outside the loop — they only run once, after the Agent has finished walking all 10 blocks.

Fix: indent destroy and place so they line up with agent.move. That puts them inside the loop, so they run every iteration.

Try the broken version first to confirm only one block appears. Then try the fixed version below:

def on_chat():
    agent.set_item(STONE, 64, 1)
    for i in range(10):
        agent.move(FORWARD, 1)
        agent.destroy(DOWN)
        agent.place(DOWN)
player.on_chat("oops", on_chat)
In Python, indentation is the law. Lines indented the same amount are a group that belongs together. Different indentation = different group. This is how Python knows what is "inside the loop" and what is not. MakeCode does this automatically when you drag blocks, but when you type code, you control the indentation.

STEP 7 Loops Inside Loops (Nested Loops)

You can put a loop inside another loop. The inside loop finishes completely before the outside loop moves on to its next iteration. This is exactly what you need to draw a square: walk one side, turn; walk another side, turn; ...repeat 4 times.

def on_chat():
    agent.set_item(STONE, 64, 1)
    for side in range(4):
        for step in range(5):
            agent.move(FORWARD, 1)
            agent.destroy(DOWN)
            agent.place(DOWN)
        agent.turn(LEFT_TURN)
player.on_chat("square", on_chat)

How to read this nested loop

  • The outer loop (for side in range(4):) repeats 4 times — one per side of the square.
  • The inner loop (for step in range(5):) repeats 5 times — one stone per step along that side.
  • Total stones placed: 4 × 5 = 20.
  • After the inner loop finishes one side, agent.turn(LEFT_TURN) rotates the Agent for the next side.
Predict — if you change range(5) to range(7), what changes? What if you change range(4) to range(3)?

range(7) → each side becomes 7 blocks long instead of 5. The square gets bigger.

range(3) → the Agent only walks 3 sides and stops — you get a triangle-ish open shape, not a closed square. (You need 4 sides for a square.)

Run the square command and watch the Agent draw the outline of a 5×5 square. Try changing the numbers and see what shapes you can make.

CHALLENGE Build Something with Repetition

Now create an original Agent program that uses at least one loop. Pick one of the options below — or invent your own.

Option A — Long Path

A straight path or trail of at least 15 blocks. Try using something other than stone (e.g., DIRT, PLANKS_OAK, or GLASS).

Option B — Wall or Tower

Use UP instead of DOWN with agent.place, or use a nested loop to stack rows. Build a wall of stone.

Option C — Custom Shape

Use a nested loop to draw a rectangle, an open box, a staircase, or any pattern with repeating sections.

Minimum requirements

  • Your program starts with a chat command.
  • Your program uses at least one for loop.
  • The Agent places at least 10 blocks in total.
  • You changed the loop count at least once and can describe what changed.
  • The final program is different from the starter examples on this page.

Stretch goals

  • Use a nested loop to build a 2D shape (a square outline, a rectangle, or a filled grid).
  • Use multiple block types — load slots 2 and 3 with different materials and switch which slot you place from.
  • Add a # comment above each loop explaining what it does.
  • Make a recognizable structure: a tic-tac-toe grid, a chess-board pattern, a plus sign, a letter of the alphabet.

SUBMIT Turn In Your Work

When you've finished the challenge, submit three items to the Schoology assignment:

  1. Screenshot of your code in the MakeCode editor. Make sure your for loop is visible.
  2. Screenshot of the result in Minecraft — the path, wall, square, or pattern your Agent built.
  3. Short written reflection. Answer all four in 1–2 sentences each:
    • What does your loop repeat, and how many times?
    • Which number did you change to make the result bigger or smaller? What happened?
    • Describe one bug you ran into — what was wrong, and how did you figure it out?
    • How would this program look if you wrote it without a loop?
Important: The reflection counts for half the grade. Show that you understand why the loop did what it did — not just that it worked.

When You're Done

  • Trade computers with a classmate and run each other's programs.
  • Try a 3D nested loop: a loop inside a loop inside a loop to fill a cube.
  • Try using agent.move(UP, 1) at the end of each row to build a tall wall instead of a flat one.
  • Explore the other Agent commands in MakeCode (agent.detect, agent.attack).
Back to Minecraft Lessons