Tutorial 2: Bearcu's Adventure

Learning Goal: For Loops

In Tutorial 1, you wrote bearcu.move_forward() multiple times. That works, but it's repetitive!

Loops let you repeat commands without copying and pasting. A for loop with range(3) will run the indented code 3 times.

for i in range(3):
    bearcu.move_forward()  # This runs 3 times!

Tutorial 2: Bearcu's Adventure

Bearcu needs to navigate around the walls to reach the star! Instead of writing move_forward() many times, you can use a 'for loop' to repeat commands. The code already has a loop that moves forward 2 times. You'll need to add turns and more loops to reach the goal!

Initializing Python environment...

Loading grid...

What You Learned

  • For loops repeat code a specific number of times
  • range(n) creates a sequence from 0 to n-1
  • Indentation matters! Code inside the loop must be indented
  • You can combine loops with other commands like turn_left()