Teacher's Guide

Chapter 2 / Level 3: Lambda Functions

Teaching Objective:

  • Student understands what a lamba functions differ from custom functions
  • Understands lambda function syntax and implementations
  • Learns how to best use lambda functions to optimize and clean up code

Guide

In this exercise we are going to be working with Lambda functions, these are in essence a simpler way to put together custom functions without needing to pre-define them at the start of the code or write a more complex code structure. It’s worth noting, this is done for simpler functions rather than more complex functions.

The overall objective of the level is to estimate how many consumables are going to be harvested this season so they may be packed and shipped. There are two types of produce being considered: "fruits" and "berries” .

First create a lambda function named calculate which adds three (3) numbers together and then multiplies them by 2 . This is used to predict the number of items that will grow this season. Follow that up with creating another lambda function named estimate , this is used to display your product calculation from the previous lambda function. Lastly create a lambda function named total which adds two values together and subtracts a third value named loss.

calculate = lambda x,y,z : (x + y + z) * 2

estimate = lambda number , name : player.speak("I estimate %d %s this season"
% (number , name) )

total = lambda num1, num2, loss : ( (num1 + num2) / loss )

Walk to the dark X mark and count how many of each fruit there is on the field.

oranges = # number of oranges in the field
pears = # number of pears in the field
apples = # number of apples in the field

At the X mark, create a variable named fruit_number and use the calculate() lambda function with the fruit variables you created earlier. Use the estimate() function to display the stored variable along with the type of produce you’re estimating which is "fruits" .

fruit_number = calculate(apples,oranges,pears)

estimate( fruit_number , "fruits" )

Afterwards walk to the light X mark next to the berry bushes and count how many of each berry color bush we have.

red = # number of red berry bushes
blue = # number of blue berry bushes
purple = # number of purple berry bushes

Once counted and the numbers established, create a variable named berry_number and use the calculate() lambda function with the berry bush quantities. Use the estimate() function to display the stored variable along with the type of produce you’re estimating which is "berries” .

berry_number = calculate(red,blue,purple)
estimate( berry_number , "berries" )

Afterwards, walk into the storehouse and make your way to the gold X mark in front of the table. Use the speak() function to check the contents of a constant in this level named doc which hold the estimates of last month’s lost product.

player.speak(doc)

Finally, create a variable named season and use the total() lambda function, with the fruit and berry calculations as well as the info on the doc, to estimate the total amount of product this season. In order to complete the level, walk to the X mark over the purple carpet and use the estimate() lambda function on the season variable and the string "products" .

season = total(berry_number,fruit_number, enter number found in doc )
estimate( season , "products" )

Map overview and path

Map overview and path

Code Solution

import player

calculate = lambda x,y,z : (x + y + z) * 2

estimate = lambda number , name : player.speak(
"I estimate %d %s this season" % (number,name) )

total = lambda num1, num2, loss : ( (num1 + num2) - loss )

player.turn_right()
player.move_forward(10)
oranges = 4
pears = 4
apples = 2
fruit_number = calculate(apples,oranges,pears)
estimate( fruit_number , "fruits" )
player.turn_right()
player.move_forward(3)
red = 5
blue = 3
purple = 4
berry_number = calculate(red,blue,purple)
estimate( berry_number , "berries" )
player.move_forward(4)
player.speak(doc)
player.turn_right()
player.move_forward()
player.turn_left()
season = total(berry_number,fruit_number, 14)
estimate( season , "products" )

Chapter 2 / Level 3: Lambda Functions | Teacher's Guide