Teacher's Guide
Chapter 5 / Level 1: Object Inheritance
Teaching Objective:
- Student understands what parent and child classes are
- Learns the proper syntax for object inheritance
- Understands how useful having parent and child classes are for optimizing code
Guide
In this exercise we’ll be working with object inheritance, this is the practice of creating objects using classes that have inherited properties from other classes. How it works is that you have a parent class that holds core properties and a child class that adopts those core properties in addition to it’s own unique set of properties.
For this exercise we’ll be using objects inheritance for bottling different types of wine, the classes being outlined as follows:
# Parent Class
class wine_bottle:
color = ""
def set_bottle(self):
player.place("empty bottle")
# Child Class
class fill_bottle(wine_bottle):
def fill_bottle(self):
player.combine(["empty bottle", self.color + " wine"])
player.speak("The bottle's color is: %s" % self.color)
player.collect(self.color + "bottle")
First walk over and grab the empty bottles at the top of the map and make your way to the dark X marks over the colored carpets. There are four (4) different wines to be bottled that correspond to the colored carpets on the X marks: "green" , "blue" , "purple" and "yellow" . At the X marks, create an object with the name of the color and bottle, set the color of the object and activate the functions from the child class.
green_bottle = fill_bottle()
green_bottle.color = "green"
green_bottle.set_bottle()
green_bottle.fill_bottle()
.................
purple_bottle = fill_bottle()
purple_bottle.color = "purple"
purple_bottle.set_bottle()
purple_bottle.fill_bottle()
.................
yellow_bottle = fill_bottle()
yellow_bottle.color = "yellow"
yellow_bottle.set_bottle()
yellow_bottle.fill_bottle()
.................
blue_bottle = fill_bottle()
blue_bottle.color = "blue"
blue_bottle.set_bottle()
blue_bottle.fill_bottle()
Once the wine has been bottled, walk over to the light X mark and open() the door into the wine cellar.
player.open()
And to complete the level, make your way to the gold X mark, store the wines you created inside a list named bottles and place() the bottles in the wine rack.
bottles = ["green bottle", "purple bottle", "blue bottle", "yellow bottle"]
player.place(bottles)
Map overview and path

Code Solution
import player
# Set up Parent Class
class wine_bottle:
color = ""
def set_bottle(self):
player.place("empty bottle")
# Set up Child Class
class fill_bottle(wine_bottle):
def fill_bottle(self):
player.combine(["empty bottle", self.color + " wine"])
player.speak("The bottle's color is: %s" % self.color)
player.collect(self.color + "bottle")
player.turn_right()
player.move_forward(3)
player.turn_right()
player.move_forward()
player.turn_left()
player.move_forward(3)
player.turn_left()
player.move_forward(5)
player.turn_right()
player.move_forward()
player.turn_right()
green_bottle = fill_bottle()
green_bottle.color = "green"
green_bottle.set_bottle()
green_bottle.fill_bottle()
player.turn_right()
player.move_forward(2)
player.turn_left()
purple_bottle = fill_bottle()
purple_bottle.color = "purple"
purple_bottle.set_bottle()
purple_bottle.fill_bottle()
player.turn_right()
player.move_forward()
player.turn_right()
player.move_forward(4)
player.turn_right()
player.move_forward(4)
player.turn_right()
yellow_bottle = fill_bottle()
yellow_bottle.color = "yellow"
yellow_bottle.set_bottle()
yellow_bottle.fill_bottle()
player.turn_left()
player.move_forward()
player.turn_right()
blue_bottle = fill_bottle()
blue_bottle.color = "blue"
blue_bottle.set_bottle()
blue_bottle.fill_bottle()
player.turn_right()
player.move_forward(7)
player.turn_left()
player.move_forward()
player.open()
player.move_forward(3)
player.turn_right()
player.move_forward(2)
player.turn_left()
bottles = ["green bottle", "purple bottle", "blue bottle", "yellow bottle"]
player.place(bottles)