Teacher's Guide

Chapter 2 / Level 5: Closures

Teaching Objective:

  • Student understands how Closures are different from regular nested functions
  • Learns the syntax of closures
  • Understands the use cases and implementations of closures

Guide

In this exercise we’re going to be working with closures, these are nested functions designed to encapsulate data, this reduces the amount of variables you need to use and hides the data within the function so it can’t be manipulated externally.

The objective in the level is to regulate the water levels in tanks so water can be distributed throughout the farm. First have a multiplier nested function set up, this will create a nested loop that will allow you to multiply two numbers entered into it.

def multiplier_of(n):
    def multiplier(number):
        return number*n
    return multiplier

Afterwards enclose the nested functions by creating closures, this allows us to multiply numbers by specific amounts without doing extra steps.

multiply_by_2 = multiplier_of(2)

multiply_by_3 = multiplier_of(3)

multiply_by_5 = multiplier_of(5)

There is a dictionary constant in this level named tanks , as an index it holds six (6) colors: “red" , “blue" , “orange" , “purple", “yellow" and “green" . These indexes hold values for the water streams in each tank flank.

Set up a custom function to regulate the water that uses the dictionary constant and closures as arguments so you can set up the flow.

def regulate_water(color , multiplier ):

  player.speak(
  "Stream set to %s units, water regulated to %s gallons."
  % (tanks[color], multiplier(tanks[color])) )

Walk to each of the dark X marks above the colored carpets and face the tanks, use the regulate_water() custom function and add the color of the carpet under the X mark and a multiplier closure that corresponds to the colored carpet.

regulate_water( "red" , multiply_by_2 )

The corresponding closures to each of the colors is as follows. Use function at all dark X marks in order to complete the level.

multiply_by_2 - “red" and “green"
multiply_by_3 - “blue" and “purple"
multiply_by_5 - “orange" and “yellow"

Map overview and path

Map overview and path

Code Solution

import player

def multiplier_of(n):
    def multiplier(number):
        return number*n
    return multiplier

multiply_by_2 = multiplier_of(2)

multiply_by_3 = multiplier_of(3)

multiply_by_5 = multiplier_of(5)

def regulate_water(color , multiplier ):

  player.speak(
  "Stream set to %s units, water regulated to %s gallons." 
  % (tanks[color], multiplier(tanks[color])) )
  
player.turn_left()
player.move_forward(2)
player.turn_left()

regulate_water( "red" , multiply_by_2 )

player.turn_right()
player.move_forward(2)
player.turn_left()

regulate_water( "blue" , multiply_by_3 )

player.turn_right()
player.move_forward(2)
player.turn_left()

regulate_water( "orange" , multiply_by_5 )

player.turn_right()
player.move_forward(2)
player.turn_left()

regulate_water( "blue" , multiply_by_3 )

player.turn_right()
player.move_forward()
player.turn_left()
player.move_forward(6)

player.turn_left()
player.move_forward()
player.turn_right()

regulate_water( "purple" , multiply_by_3 )

player.turn_left()
player.move_forward(2)
player.turn_right()

regulate_water( "yellow" , multiply_by_5 )

player.turn_left()
player.move_forward(2)
player.turn_right()

regulate_water( "green" , multiply_by_2 )

player.turn_left()
player.move_forward(2)
player.turn_right()

regulate_water( "purple" , multiply_by_3 )
Chapter 2 / Level 5: Closures | Teacher's Guide