Teacher's Guide

Chapter 2 / Level 1: Polymorph Functions

Teaching Objective:

  • Students learn what polymorphic functions are
  • Understand how they can be used to optimize code
  • Understand how using [*] as an operator can be used to manipulate strings

Guide

In this exercise we will be working with polymorphic functions, these are functions that are universal in the sense that they can be used with multiple different data types. In particular we will be working with the len() function, designed to calculate the number of items in an object such as a list or dictionary as well as the number of characters in a string.

To start off, walk to the dark X mark in front of the stable and put the name of the piglets: "Peggle", "Peter", "Piper" and "Pedro", in a list named piglets . Create a variable named number and calculate the length of the list using len() . Afterwards use the speak() function to count and display how many piglets there are on the stable by using code.

piglets = ["Peggle", "Peter", "Piper", "Pedro"]
number = len(piglets)
player.speak("There are %d Piglets" % (number) )

Once that is done, make your way to the light X mark in front of the crop field and create a dictionary named crops . Add the index name and number of each of the crops present in the field, their names being: "Tomatoes" , "Pumpkins" , "Eggplant" . Follow that up by using the speak() function to calculate the length of the dictionary in code and display it.

crops = {}
crops["Tomato"] = 4
crops["Pumpkin"] = 2
crops["Eggplant"] = 3
player.speak("There are %d types of crops" % ( len(crops) ) )

Next walk over to the dark X mark on the purple mat by the shed to determine the number of orders of crops needed to ship out by using the variable constant order . Use speak() and unpack the constant by setting it in brackets and adding an asterisk ahead of it [*order] . Use the speak() function again to calculate and display the number of orders going out.

player.speak( [*order] )
player.speak("There are %d orders of crops going out" % ( len(order) ) )

Walk to the gold X mark in order to determine how many pounds of food need to be delivered for the piglets. Create a variable named food and multiply the previously created variable number by 35 to determine the pounds of food needed. Finally, use the speak() function to display the amount of food being delivered and complete the level.

Map overview and path

Map overview and path

Code Solution

import player

player.move_forward()
player.turn_right()
player.move_forward(3)
player.turn_left()
piglets = ["Peggle", "Peter", "Piper", "Pedro"]
number = len(piglets)
player.speak("There are %d Piglets" % (number) )
player.turn_right()
player.move_forward(5)
player.turn_left()
crops = {}
crops["Tomato"] = 4
crops["Pumpkin"] = 2
crops["Eggplant"] = 3
player.speak("There are %d types of crops" % ( len(crops) ) )
player.turn_right()
player.turn_right()
player.move_forward(2)
player.speak( [*order] )
player.speak("There are %d orders of crops going out" % ( len(order) ) )
player.turn_right()
player.move_forward(3)
player.turn_left()
food = number * 35
player.speak("There are %d pounds of food coming in" % (food) )
Chapter 2 / Level 1: Polymorph Functions | Teacher's Guide