Loops, Repetition, and Agent Patterns
Make the Agent repeat actions automatically with for loops.
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.
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
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.
- In the Minecraft world, press C on your keyboard to open the Code Builder.
- In the Code Builder window, choose MakeCode.
- Click New Project and give it a name —
Lesson 2 - Loopsworks. - Click Code options, then select Python Only.
- 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)
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
- Switch to Minecraft.
- Open chat (T), type
path, press Enter. - 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
- Change
range(5)torange(15). Run the program. Did you get 15 blocks? - Change
range(15)back to a smaller number, likerange(3). - Change
agent.move(FORWARD, 1)toagent.move(FORWARD, 2). What happens to the spacing of the stone blocks? (Hint: this is a common bug.) - Add an
agent.turn(RIGHT_TURN)call after the place line but still inside the loop. What pattern does that create?
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)
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.
Minimum requirements
- Your program starts with a chat command.
- Your program uses at least one
forloop. - 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:
- Screenshot of your code in the MakeCode editor. Make sure your
forloop is visible. - Screenshot of the result in Minecraft — the path, wall, square, or pattern your Agent built.
- 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?
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).