Teacher's Guide
Chapter 2 / Level 4: Nested Functions
Teaching Objective:
- Student learns how to set up nested functions
- Understands the uses of having functions inside other functions and interface together
- Learns some use cases of nested functions and it’s implementations
Guide
In this exercise we will be working with nested functions which are functions that exist inside other functions. We will be setting up a nested function named operate_machine() for operating a cloth making machine, this allows us to collect cloth that has already been produced, and place thread in machines that require more materials to produce cloth.
The function is written in the code editor is incomplete and in order to properly set it up you must add collect() and place() code inside the nested functions inside. On collect_cloth() add the line player.collect("cloth") and on place_thread() add the line player.place("thread") .
def operate_machine(color):
def collect_cloth():
player.collect("cloth")
player.speak("Cloth is ready for storage")
def place_thread():
player.place("thread")
player.speak("Machine needed more materials")
if color == "green":
collect_cloth()
if color == "red":
place_thread()
After the function is fully set up, walk to the light X mark and use the collect function to grab the thread needed to operate the machines.
player.collect("thread")
Walk to the dark X marks by the machines in workshop and use the custom function operate_machine() . There are several machines present in the field and each have either a "red" or "green" carpet in front of it, add the color of the carpet you are standing on as an argument to the function in order to have the machines perform their appropriate functions.
operate_machine("green")
....
operate_machine("red")
....
# Do this for all colors at their positions
The function will allow you to collect cloths that are ready from the machine. Create a variable named cloths and store the number of cloths you’ve collected. Afterwards make your way to the gold X mark and place() the variable in order to complete the level.
cloths = # of cloths collected
....
player.place(cloths)
Map overview and path

Code Solution
import player
def operate_machine(color):
def collect_cloth():
player.collect("cloth")
player.speak("Cloth is ready for storage")
def place_thread():
player.place("thread")
player.speak("Machine needed more materials")
if color == "green":
collect_cloth()
if color == "red":
place_thread()
player.turn_right()
player.move_forward(2)
player.turn_right()
player.move_forward(4)
player.collect("thread")
player.turn_left()
player.move_forward(4)
player.turn_right()
operate_machine("red")
player.turn_right()
player.move_forward()
player.turn_left()
operate_machine("green")
player.turn_right()
player.move_forward()
player.turn_left()
operate_machine("red")
player.turn_right()
player.move_forward(4)
player.turn_left()
operate_machine("green")
player.turn_right()
player.move_forward()
player.turn_left()
operate_machine("red")
player.turn_right()
player.move_forward()
player.turn_left()
operate_machine("green")
cloth = 3
player.turn_left()
player.move_forward(3)
player.turn_right()
player.move_forward(3)
player.turn_right()
player.place(cloth)