Teacher's Guide
Chapter 7 / Level 6: Using Operators in Functions
Teaching Objective:
- Student learns how to construct more complicated functions
- Understands how to set up multiple operators in a custom function
- Learns how to optimize functions in order to be able to reuse them for multiple operations
Guide
In this exercise you must use a custom function to check if you have enough berries to mash for a serving of puree. A custom function is used because there are three types of berries: red berries , blue berries and black berries , the custom function allows you to check all three without having to write additional code.
First create a variable named serving and assign the value to be the total amount of berries available divided by the amount of types of berries.
serving = 12 / 3
Once the variable is set up create a custom function named mash_berries that takes a number argument and checks if the amount is enough to produce a serving size of puree. Inside the function there will be conditions displaying different messages depending on the input.
def mash_berries(item):
if item > serving:
player.speak("You have too many berries")
player.speak("You have %d more than needed" % (item - serving) )
if item == serving:
player.speak("You have the right amount of berries")
if item < serving:
player.speak("You have too few berries")
player.speak("You need %d more for a serving" % ( serving - item) )
Once the variable and custom function are set up, grab all the berries in the field and store their amounts in the variables: red_berries , blue_berries and black_berries.
red_berries = 4
blue_berries = 3
black_berries = 5
Once you have the variables set up, walk to the X marks and use the custom function with each of the berry variables to check if they can produce a serving size of puree and complete the level.
mash_berries(black_berries)
.........
mash_berries(red_berries)
.........
mash_berries(blue_berries)
Map overview and path

Code Solution
import player
serving = 12 / 3
def mash_berries(item):
if item > serving:
player.speak("You have too many berries")
player.speak("You have %d more than needed" % (item - serving) )
if item == serving:
player.speak("You have the right amount of berries")
if item < serving:
player.speak("You have too few berries")
player.speak("You need %d more for a serving" % ( serving - item) )
player.turn_left()
player.move_forward(8)
player.turn_left()
player.move_forward(2)
player.turn_left()
player.move_forward(8)
player.turn_right()
player.move_forward(2)
player.turn_right()
player.move_forward(8)
red_berries = 4
blue_berries = 3
black_berries = 5
player.turn_left()
player.move_forward()
player.turn_right()
player.move_forward()
mash_berries(black_berries)
player.turn_right()
player.move_forward(2)
player.turn_left()
mash_berries(red_berries)
player.turn_right()
player.move_forward(2)
player.turn_left()
mash_berries(blue_berries)