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

Advanced Functions
Partial Functions

Objective

Pack and place building materials that were delivered to the dock by using partial functions.

Some supplies were just delivered to a dock, they should be packed and stored so they can be used as construction materials. The materials need to be wrapped in cloth and tied up with thread. It’s important to weigh all the items when you pack them so they can be transported effectively later. In order to facilitate this we can optimize this process by using Partial Functions.

A Partial Function is a custom function that is shortened by not needing to add argument values that are largely consistent. This allows you to use functions that take multiple arguments and execute them with only a single argument or whatever number you choose.

from functools import partial

To start off you must import partial from the module known as functools , this will allow you to create Partial Functions. Importing code always goes at the top of the code editor.

Set up a function named weight and have it take three(3) arguments: thread , cloth and material. This function will allow you to calculate the weight of the packed materials, we can optimize this process further by funneling it through a partial function.

def weight(thread, cloth, material): return 2 * thread + 5 * cloth + material * 10 package = partial(weight, 4, 2)

Create a Partial Function named package, it’s set up in a similar process as lambda functions. Use partial() to populate the function and add the name of the function as well as fixed values for the arguments you'd like to assign.

Any arguments from the original function you don’t assign will become the arguments that must be assigned by the Partial Function. In the case of the package() Partial Function, it’s the material argument.

Walk to the dark X marks in front of crates to grab packing materials. On the yellow carpet X mark, use the collect() function to acquire "thread" and walk to the purple carpet X mark and use the collect() function to acquire "cloth". These items will be used for packing the materials.

Walk across the dock and grab all the construction materials, these are: bricks , planks, red_bricks and wood. Once you collect them, add the number you’ve collected of each to a variable named after the material, for example: bricks = 3 .

Afterwards walk to the light X marks in front of the crates and calculate the weight of each construction material before storing it. Use the speak() function in conjunction with the package() Partial Function to calculate the weight. Use the material variables as arguments for the package() function, for example:

player.speak("Package weighs %s pounds" % package(bricks)) player.place(package(bricks))

Follow this up by using the place() function to store the material inside the crates, do so using the package() function as well, like in the example above.

On the light X mark with the red carpet weigh and store bricks . On the light X mark with the blue carpet weigh and store planks . On the light X mark with the orange carpet weigh and store red_bricks . On the light X mark with the green carpet weigh and store wood. Store and weigh all four(4) materials in the correct crates in order to complete the level.

Code book