Teacher's Guide
Chapter 6 / Level 4: Break and Continue Statements
Teaching Objective:
- Student learns how to use break & continue statements
- Learns how to implement loops with complex conditions
- Understands how to better optimize code using conditions
Guide
In order to complete this level, the student must gather all weeds in the passage and dump them. Use loops with conditions to automate the process, and make the code more efficient.
First gather all the weeds in the passage by using a while loop, create a variable named bag and have the while loop automatically fill up the bag until it has picked up all the weeds. There are ten(10) weeds in total, have a condition inside the while loop to check if the player has grabbed all ten(10) weeds and use the break operator to end the loop if the condition is met.
while True:
player.move_forward()
bag += 1
if bag == 10:
break
Once the weeds are grabbed use a for loop to dump the weeds inside containers by using the place() function on the X marks. There is a gap between the two (2) containers so in order to execute this function with a loop you must use the continue operator. This allows you to skip code if the condition is met.
for x in range(3):
player.move_forward()
if x == 1:
continue
player.turn_right()
player.place(bag/2)
player.turn_left()
Use the if statement to check if loop cycle has reached 1 , which would take place in the middle ground between the two containers, and if so skip the cycle. Since there are two containers, spit the bag variable in half when using the place() function, like this: player.place(bag/2) .
Once the weeds have been collected and dumped, head for the exit marked by a star in order to complete the level.
Map overview and path

Code Solution
import player
bag = 0
player.turn_left()
while True:
player.move_forward()
bag += 1
if bag == 10 :
break
player.move_forward()
player.turn_right()
for x in range(3):
player.move_forward()
if x == 1:
continue
player.turn_right()
player.place(bag/2)
player.turn_left()
player.move_forward()
player.turn_right()
player.move_forward(5)