Teacher's Guide

Chapter 6 / Level 5: Loop Conditions

Teaching Objective:

  • Student understands how to format conditions inside loops
  • Understands how to structure for loops to perform variable tasks

Guide

In this exercise you must collect all all weeds and dump them using no more than twelve (12) lines of code. To do this, use loops with proper conditions in order to efficiently navigate the level and complete the task.

First create a for loop with a range() of three (3) since there are three passages to navigate. The first passage has five (5) weeds, the rest have nine (9).

for x in range(3):
    player.move_forward(10)
    if x == 0:
        bag = 5
        player.place(bag)
        player.turn_right()
    else:
        bag = 9
        player.place(bag)
        player.turn_right()

Have the player move the length of the passage and set up a condition to check if the player is in the first passage. If so, set up an if statement to have the player only put five (5) weeds in the variable bag and use the place() function to dump the weeds at the X mark. Set up an else statement and have a variant of the code place nine (9) weeds in the bag instead if the condition is not met.

Follow the process described and you will be able to complete the level using no more than twelve (12) lines of code.

Map overview and path

Map overview and path

Code Solution

import player

player.turn_right()

for x in range(3):
    player.move_forward(10)
    if x == 0:
        bag = 5
        player.place(bag)
        player.turn_right()
    else:
        bag = 9
        player.place(bag)
        player.turn_right()
Chapter 6 / Level 5: Loop Conditions | Teacher's Guide