for loops to repeat actions in Minecraft.player.sayInstead of copying the same line over and over, we can use a loop to repeat it.
Sample Code:
for i in range(5):
player.say("This is message number " + str(i))
Discussion:
range(5) means the loop runs 5 times: 0, 1, 2, 3, 4.i is the loop variable that changes each time.Let’s build a straight line of blocks in front of the player.
Sample Code:
# Build a line of 10 stone blocks along the X-axis
for x in range(10):
blocks.place(STONE, pos(x, 0, 0))
Try:
PLANKS_OAK, GLASS).pos(x, 0, 0) to pos(0, 0, z)).This example uses a loop inside a loop (nested loops) to build a wall.
Sample Code:
# Build a 10x3 wall (10 wide, 3 high)
for x in range(10): # width
for y in range(3): # height
blocks.place(BRICKS, pos(x, y, 0))
Discussion:
Modify the wall to create a doorway by skipping some blocks in the middle.
Hint: Use an if statement inside the loop, for example:
for x in range(10):
for y in range(3):
# Skip blocks at the center (doorway)
if not (x == 4 or x == 5) or y > 1:
blocks.place(BRICKS, pos(x, y, 0))
Due Date: December 3, 2025
Submit it to Google Classroom.
Save your file as PX_L02_lastname.png.