Teacher's Guide

Chapter 4 / Level 4: For Loop Sequence

Teaching Objective:

  • Student learns how to make dynamic loop loop code
  • Understands how much looping can assist with code optimization as the exercise needs to be completed with only 4 lines of code

Guide

In order to complete this level the student must be able to make full use of the for loop’s variable, in order to make your way through the maze and collect all the grains in under four (4) lines of code.

The variable in question is usually named x however can be named whatever you like so long as it’s used correctly. This variable starts off at 0 and increments by one (1) each cycle of a loop.

for x in range(11):
    player.move_forward(x + 1)
    player.turn_left()

By adding the variable to the function move_forward() this will allow you to increase the number of steps you take each cycle. By adding a +1 you have the movement code start off at 1 instead of 0.

By executing this loop the correct number of cycles you will be able to collect all the grains and complete the level.

Map overview and path

Map overview and path

Code Solution

import player

for x in range(11):
    player.move_forward(x + 1)
    player.turn_left()
Chapter 4 / Level 4: For Loop Sequence | Teacher's Guide