Teacher's Guide
Chapter 2 / Level 6: Partial Functions
Teaching Objective:
- Student learns the syntax of Partial Functions
- Understands when to best use Partial functions and optimize code
- Leans some use cases and implementations of Partial Functions
Guide
In this exercise we will be working with Partial Functions, these are designed to optimize existing functions by shortening them and their number of arguments.
The goal of the level is to weigh and pack construction materials to store them in crates by using Partial Functions. Start off by importing the partial() function from functools in order to execute Partial Functions. Follow this up by creating a function named weight() which calculates the weight of three (3) items: thread, cloth, material .
from functools import partial
def weight(thread, cloth, material):
return 2 * thread + 5 * cloth + material * 10
package = partial(weight, 4, 2)
Given that the weight of the thread and cloth items are fairly constant, we can provide them a fixed value by consolidating the weight() function into a Partial Function and only modifying the material argument instead. This is how it’s set up in the code above wit the package() Partial Function.
Walk to the dark X marks in front of crates and collect() "thread" at the yellow carpet and "cloth" at the purple carpet.
player.collect("thread")
.........
player.collect("cloth")
Walk across the dock and grab bricks , planks, red_bricks and wood . Create variables for each and store the amounts collected of each item.
bricks = 3
planks = 6
red_bricks = 4
wood = 5
Walk to the light X marks and use speak() and place() functions in order to determine each packed item’s weight and store them by using the package() Partial Function and using the item variables with it.
player.speak("Package weighs %s pounds" % package(bricks))
player.place(package(bricks))
Do this for all four (4) items in order to complete the level. 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.
Map overview and path

Code Solution
import player
from functools import partial
def weight(thread, cloth, material):
return 2 * thread + 5 * cloth + material * 10
package = partial(weight, 4, 2)
player.turn_right()
player.move_forward(6)
player.turn_right()
player.move_forward()
player.collect("thread")
player.turn_left()
player.move_forward()
player.turn_right()
player.collect("cloth")
player.turn_left()
player.move_forward(3)
player.turn_right()
player.move_forward(2)
player.turn_right()
player.move_forward(5)
player.turn_right()
player.move_forward(2)
player.turn_left()
player.move_forward(2)
player.turn_left()
player.move_forward(3)
bricks = 3
planks = 6
red_bricks = 4
wood = 5
player.speak("Package weighs %s pounds" % package(bricks))
player.place(package(bricks))
player.turn_left()
player.move_forward(2)
player.turn_right()
player.speak("Package weighs %s pounds" % package(planks))
player.place(package(planks))
player.turn_left()
player.move_forward(2)
player.turn_right()
player.speak("Package weighs %s pounds" % package(red_bricks))
player.place(package(red_bricks))
player.turn_left()
player.move_forward(2)
player.turn_right()
player.speak("Package weighs %s pounds" % package(wood))
player.place(package(wood))