Teacher's Guide
Chapter 6 / Level 2: Optimizing Decorators
Teaching Objective:
- Student learns more about the syntax of decorators and how to best optimize them
- Learns how to use the @ symbol in the context of decorators and how to pre-decorate functions
- Learns more use cases and circumstances for using decorators
Guide
In this exercise we’ll be optimizing decorators, by using the @ symbol along with the name of the decorator and write the code in the line above the function you would like to decorate you can automatically attach the decorator to that function.
# Decorator
def create_pizza(func):
def set():
pizza = ["dough","red sauce","cheese"]
player.combine(pizza)
func()
return set
# Function being decorated with @
@create_pizza
def peperoni_pizza():
player.place("Peperoni")
player.speak("Peperoni Pizza has been prepped")
# Using pre-decorated function
peperoni_pizza()
Your objective in this level is to collect ingredients and use them to put together different types of pizzas so they may be cooked in the ovens. Walk to the gold X mark in order to collect ingredients for the pizza, there is a Matrix that holds all items inside the shelves in front of the X mark.
Create a list named ingredients and use a List Comprehension to acquire all the ingredients you need and exclude "jar" from the list. From here use speak() to display all the ingredients you’re going to be using to make pizzas.
shelves = [
["cheese", "dough", "jar"],
["jar", "red sauce", "jar"],
["peperoni", "jar" , "ham"],
["olive", "pepper", "onion"]
]
ingredients = [
item for row in ( shelves )
for item in row if not ( "jar") in item]
player.speak(ingredients)
Once you have all the necessary ingredients in the list, add the missing functionality in the decorated functions provided in the code editor. With the blank place() functions: peperoni_pizza() requires: "peperoni" added, vegetable_pizza() which requires: "olive", "pepper", "onion" added , and pineapple_pizza() which requires: "ham" and "pineapple" added.
@create_pizza
def pineapple_pizza():
player.place("ham")
player.place("pineapple")
player.speak("Pineapple Pizza has been prepped")
@create_pizza
def vegetable_pizza():
player.place("olive")
player.place("pepper")
player.place("onion")
player.speak("Vegetable Pizza has been prepped")
Once they’ve all been added, walk to the dark X marks with the colored carpets and perform the following: on red carpets create peperoni_pizza() , on green carpets create vegetable_pizza() and on yellow carpets create pineapple_pizza() by using the functions. On your way to the yellow carpet X mark, head to the light X mark, face the cabinet and collect() function to grab "pineapple" . Complete the level by creating all 5 pizzas.
Map overview and path

Code Solution
import player
def create_pizza(func):
def set():
pizza = ["dough","red sauce","cheese"]
player.combine(pizza)
func()
return set
@create_pizza
def peperoni_pizza():
player.place("peperoni")
player.speak("Peperoni Pizza has been prepped")
@create_pizza
def pineapple_pizza():
player.place("ham")
player.place("pineapple")
player.speak("Pineapple Pizza has been prepped")
@create_pizza
def vegetable_pizza():
player.place("olive")
player.place("pepper")
player.place("onion")
player.speak("Vegetable Pizza has been prepped")
player.move_forward(2)
player.turn_right()
player.move_forward(2)
player.turn_right()
shelves = [
["cheese", "dough", "jar"],
["jar", "red sauce", "jar"],
["peperoni", "jar" , "ham"],
["olive", "pepper", "onion"]
]
ingredients = [
item for row in ( shelves )
for item in row if not ( "jar") in item]
player.speak(ingredients)
player.turn_right()
player.move_forward(2)
player.turn_left()
player.move_forward(3)
peperoni_pizza()
player.turn_left()
player.move_forward()
player.turn_right()
vegetable_pizza()
player.turn_left()
player.move_forward(2)
player.turn_right()
player.move_forward(2)
player.turn_left()
player.move_forward(2)
player.turn_left()
peperoni_pizza()
player.turn_left()
player.move_forward(5)
player.turn_left()
player.collect("pineapple")
player.turn_right()
player.move_forward()
player.turn_right()
vegetable_pizza()
player.turn_left()
player.move_forward(4)
player.turn_right()
pineapple_pizza()