Teacher's Guide
Chapter 6 / Level 1: Using Decorators
Teaching Objective:
- Students understand what decorators are and how they relate to functions
- Understand the syntax of a decorator and how it attaches to a function
- Learn their potential uses and implementations in code
Guide
In this exercise we’re going to be working with Decorators, these are custom functions designed to add additional functionality to an existing function. We’re going to be using Decorators in order to open()/close() oven doors that need to be loaded with wood and turned on for food prep.
Start off by setting up a Decorator and function that would allow you to open the oven place the firewood and close the oven.
# Decorator , nested function that adds functionality to a function
def load_wood(func):
def load():
player.open()
func()
player.close()
return load
# Custom function that can be decorated
def add_wood():
player.place("firewood")
player.speak("Three logs placed in oven")
# Decorate the custom function
prep_oven = load_wood(add_wood)
You will also need to set up a question function named set_temperature() , this will allow you adjust the temperature to either High or Medium.
def set_temperature():
await player.question(
"Set temperature to High instead of Medium?", "answer")
if answer == true:
player.speak("Temperature set to High")
else:
player.speak("Temperature set to Medium")
Start off by collecting the firewood in the storeroom, once grabbed walk to the gold X mark and use the read() function to read the memo on the table. The memo has the temperatures of each of the ovens in relation to the colored carpets.
player.read()
Walk to the ovens where the light and dark X marks are located and use the functions set up at the start of the code. At the light X marks use the prep_oven() and at the dark X mark use set_tempature(). The pop up will ask the temperature setting for the oven, use the information in the memo to set the correct temperature according to the colored carpets.
prep_oven()
........
set_tempature()
........
set_tempature()
........
prep_oven()
Set all four(4) ovens in order to complete the level.
Map overview and path

Code Solution
import player
# Decorator, adds extra functionality to another function
def load_wood(func):
def load():
player.open()
func()
player.close()
return load
# Custom function that can be decorated
def add_wood():
player.place("firewood")
player.speak("Three logs placed in oven")
# Decorate the custom function
prep_oven = load_wood(add_wood)
# Set Question custom function
async def set_temperature():
answer = await player.question(
"Set temperature to High instead of Medium?")
if answer:
player.speak("Temperature set to High")
else:
player.speak("Temperature set to Medium")
player.turn_right()
player.move_forward(3)
player.turn_left()
player.move_forward(5)
player.turn_left()
player.move_forward(2)
player.turn_left()
player.move_forward()
player.turn_left()
player.move_forward(2)
player.turn_right()
player.move_forward(4)
player.read()
player.turn_left()
player.move_forward(3)
player.turn_right()
player.move_forward(2)
player.turn_left()
player.move_forward(2)
player.turn_right()
player.move_forward()
prep_oven()
player.turn_right()
player.move_forward()
player.turn_left()
await set_temperature()
player.turn_right()
player.move_forward()
player.turn_left()
await set_temperature()
player.turn_right()
player.move_forward()
player.turn_left()
prep_oven()
player.turn_right()
player.turn_right()
player.move_forward()
player.turn_left()
player.move_forward(4)
player.turn_left()
player.move_forward()
prep_oven()
player.turn_right()
player.move_forward()
player.turn_left()
await set_temperature()
player.turn_right()
player.move_forward()
player.turn_left()
await set_temperature()
player.turn_right()
player.move_forward()
player.turn_left()
prep_oven()