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

Decorators
Using Decorators

Custom functions have a wide range of uses, however sometimes you may want to add additional functionality to a pre-existing function. This is called a decorator, it’s a function designed to augment an existing function hence the name.

For this level the ovens need to be prepped in order to get them ready for food preparation. Use decorators to open() and close() in between loading firewood into the ovens.

Guide

Objective

Fill ovens with firewood and set the ovens so they can be prepped for cooking by using decorators.

The brick ovens need to be readied so you can bake food for later in the evening. The ovens are off and empty and require some firewood to be lit up. Grab some firewood and have it loaded in the ovens so you may begin cooking. Wile you can do this manually or by using a custom function, however the process can be expedited by using decorators.

Decorators are add-ons that can be added to a custom function in order to expand it’s functionality on demand without needing to overcomplicate it with conditions.

# 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)

Walk to the storeroom in the bottom of the map and collect all four (4) of the log bundles. Walk to the gold X mark and use the read() function to check the memo on the bar displaying what temperatures the ovens should be set. Make note of the information in the pop up as you’ll need it later.

Follow this by walking to the light X marks by the oven and use decorated function in order to place the firewood, like this: prep_oven() . As you put the firewood in the oven walk to the dark X marks on the colored carpets use the question() async function to set the temperature of the ovens.

The supporting function will be set up beforehand, named set_temperature(), and your answer to the question will determine if the temperature will be set to High or to Medium. Set the temperature by writing the function and answering the question with the memo you read earlier, with each temperature set in accordance with the corresponding colored carpet. Remember to use await when using async fuctions, like this: await set_temperature()

Once you prep and set all four (4) ovens you will have completed the level.

Code book