Coding for KidsCoding for Kids
Creative LevelsChallengesTeacher's Guide
Vote for features
Advanced Python Development Course
Chapter
>
Level

Decorators
More complex Decorators

Objective

Set Pizza’s in oven at correct temperatures and prepare salads by using decorators with arguments.

Now that the Pizzas are already prepped it’s time to cook them in the ovens, once that’s taken care we should prepare salads to go with the pizza’s after they are done. You can accomplish these tasks by using decorators with arguments, similar to custom functions you can also pass arguments through your decorators.

The pizzas need to be placed in the oven but first you need to collect more "firewood" at the bottom left of the map. Once collected collect the basket at the counter by the left oven, this basket contains "pizza" to be baked in the ovens.

There is a function and it’s decorator are set up in the code editor, the function is used to place and bake the "pizza" in the oven, the decorator checks if the oven has the right temperature and if it isn’t the right temperature, "firewood" is placed instead.

def place_oven(func): def check(temp): if temp < 450: player.speak("Oven is not hot enough") player.open() player.place("firewood") player.close() return return func(temp) return check @place_oven def bake_pizza(temp): player.open() player.place("pizza") player.close() player.speak("I am baking Pizza at %d degrees" % (temp))

Walk to the light X marks and use the read() function while facing the oven, to identify the temperature of the oven. Follow this up by writing the decorated function bake_pizza() and add the temperature you read as an argument, for example: bake_pizza(450) .

While you go place the pizzas and firewood in the ovens, stop at the gold X mark and face the cabinet, here you can collect the ingredients to put together salads. Currently the salad ingredients are stored in a nested list that’s outlined in the code editor, however this list is messy and it would be in your best interest to have the items stored in a proper list in order to use them for your salads.

cabinet = [["lettuce"], ["carrot" ,"tomato"] , ["oil" , "ranch", "cesar"] , ["cheese"]] ingredients = [item for sublist in cabinet for item in sublist]

Create a list named ingredients and use List Comprehension to “flatten” the nested list. This is the process of removing the sub lists from the nested list so that all items are now together in one list rather that multiple smaller lists. After the list has been flattened, use the speak() function with the list verify the ingredients you’re going to be using.

After all necessary items have been placed in the ovens, and the ingredients list has been addressed, walk to the dark X marks to create the salads. In the editor a function and decorator are set up, this time they are not connected and must be manually declared, for example: salad = add_toppings(toss_salad("ranch","carrot")) . This set of functions take two (2) arguments: dressing and vegetable .

def add_toppings(func): def add(dressing, vegetable): player.place(dressing) player.place(vegetable) func(dressing, vegetable) return add @add_toppings def toss_salad(dressing, vegetable): player.combine("lettuce", dressing, vegetable) player.speak("I am preparing salad with %s dressing and %s" % (dressing, vegetable))

Each dark X mark is located on a color carpet, each one has a different salad that need’s to be created on it using the ingredients you picked from the cabinet. On the yellow carpet, create a salad using: "cesar" and "cheese" , on the green carpet create a salad using: "oil" and "tomato" , on the blue carpet create a salad using: "ranch" and "carrot" .

Once all three (3) salads are put together using the decorated function toss_salad(dressing, vegetable) in this format, the level will be complete.

Code book