Teacher's Guide
Chapter 5 / Level 4: Class Polymorphism
Teaching Objective:
- Student learns about the many uses of class Polymorphisim and it’s implementation
- Understands how to manipulate class functions to optimize code and streamline efficiency
- Learns how to integrate class techniques learned so far in order to put together comprehensive class objects
Guide
In this exercise we will be working with class polymorphism, this is a method of optimizing use of class objects by having them use functions with the same name and are used in the same manner.
In this level you need to finish setting up the mixtures for creating wine by adding together additional ingredients to the wine barrels. First walk to the light X mark and face the desk, use the read() function to check what items need to be added to the classes set up in the code editor.
player.read()
Make note of the items in the document and set up the classes: grape , cherry , strawberry , mango. Add place() functions with the corresponding items in each class’s tacke_action() functions.
class grape:
def __init__(self,item):
self.name = "grape"
self.item = item
def add(self):
player.speak("%s is added to the %s mixture." % (self.item, self.name))
def take_action(self):
player.place(self.name)
player.place("white oak")
............
player.place("black oak")
............
player.place("red oak")
............
player.place("pin oak")
Once the classes are set up, walk to the gold X mark and use the collect() function and speak() function to grab and verify the contents of a list named ingredients which has items needed to be added to the wine mixtures.
player.collect(ingredients)
player.speak(ingredients)
Walk to the bottom of the map and grab all four (4) of the bags containing the fruit an berries needed to complete the mixtures. Follow up by walking to the dark X mark over the red carped and facing the barrel.
Now we create objects for each of the mixtures and add in some of the ingredients you gathered from the list constant. The objects are to be named: grape_mix , cherry_mix , strawberry_mix and mango_mix , they house their class structures along with their arguments. For grape there’s "nothing" added, for cherry add "pure water" , for strawberry add "sugar" and for mango add "spice" .
grape_mix = grape("nothing")
strawberry_mix = strawberry("sugar")
mango_mix = mango("spice")
cherry_mix = cherry("pure water")
End the exercise by using class polymorphisms , there are two (2) loops written in the code editor, add in mixes corresponding to the colored carpets. The red carpet needs the strawberry_mix , the purple carpet needs the cherry_mix , the green carpet needs the grape_mix and the orange carpet needs the mango_mix .
On the first loop, arrange the function names in the correct sequence from left to right in accordance with the map. Move to the dark X mark with the green carpet and do the same with the second loop but instead arrange the items from right to left in order to complete the level.
for mix in (strawberry_mix, mango_mix, cherry_mix):
mix.add()
mix.take_action()
player.turn_right()
player.move_forward(2)
player.turn_left()
..........
for mix in ( grape_mix, cherry_mix, mango_mix):
mix.add()
mix.take_action()
player.turn_left()
player.move_forward(2)
player.turn_right()
Map overview and path

Code Solution
import player
# Class to make grape mixture
class grape:
def __init__(self,item):
self.name = "grape"
self.item = item
def add(self):
player.speak("%s is added to the %s mixture." % (self.item, self.name))
def take_action(self):
player.place(self.name)
player.place("white oak")
# Class to make cherry mixture
class cherry:
def __init__(self,item):
self.name = "cherry"
self.item = item
def add(self):
player.speak("%s is added to the %s mixture." % (self.item, self.name))
def take_action(self):
player.place(self.name)
player.place(self.item)
player.place("black oak")
# Class to make strawberry mixture
class strawberry:
def __init__(self,item):
self.name = "strawberry"
self.item = item
def add(self):
player.speak("%s is added to the %s mixture." % (self.item, self.name))
def take_action(self):
player.place(self.name)
player.place(self.item)
player.place("red oak")
# Class to make guava mixture
class mango:
def __init__(self,item):
self.name = "mango"
self.item = item
def add(self):
player.speak("%s is added to the %s mixture." % (self.item, self.name))
def take_action(self):
player.place(self.name)
player.place(self.item)
player.place("pin oak")
#------------------------------------------------
player.turn_right()
player.move_forward(4)
player.turn_right()
player.read()
player.turn_left()
player.move_forward(2)
player.turn_right()
player.collect(ingredients)
player.speak(ingredients)
player.turn_right()
player.move_forward()
player.turn_right()
player.move_forward(9)
player.turn_left()
player.move_forward(2)
player.turn_left()
player.move_forward(2)
player.turn_right()
player.move_forward(2)
player.turn_left()
grape_mix = grape("nothing")
strawberry_mix = strawberry("sugar")
mango_mix = mango("spice")
cherry_mix = cherry("pure water")
for mix in (strawberry_mix, mango_mix, cherry_mix):
mix.add()
mix.take_action()
player.turn_right()
player.move_forward(2)
player.turn_left()
player.move_forward(3)
player.turn_left()
player.move_forward(2)
player.turn_right()
for mix in ( grape_mix, cherry_mix, mango_mix):
mix.add()
mix.take_action()
player.turn_left()
player.move_forward(2)
player.turn_right()