Teacher's Guide

Chapter 5 / Level 3: Super Function

Teaching Objective:

  • Student understands how to use the super function and it’s syntax
  • Understands when to use it and how it enhances the child & parent classes dynamic
  • Learns how to create and use more advanced class functions by using the Super Function

Guide

In this exercise we’ll be working with the super() function, this is very similar to method overring, the process is the same but in this case you can use super() to execute the parent class function’s code in addition to any new code you write.

class cooked_meat:

  type = ""

  def cook_meat(self):
    player.speak("Cooking the %s meat with oil and onions."
    % self.type)
    player.place("meat")
    player.collect("cooked %s" % self.type)

# Set Child Class

class ready_meat(cooked_meat):
  rarity = ""

  def cook_meat(self):
    super().cook_meat()
    player.speak("The meat is cooked %s" % self.rarity)

To start off, walk into the refrigerator and collect the four (4) meats, there are four different types of meat that need to be cooked at different rarities: "pork"  cooked  "medium",  "beef" cooked "well done" , "buffalo" cooked "medium rare", and "lamb" cooked "rare" .

Walk over to the light X mark and cook the collected meats, set the type and the rarity then use the child class function:

pork_meat = ready_meat()
pork_meat.type = "pork"
pork_meat.rarity = "medium"
pork_meat.cook_meat()

# Do the same for the other meats

Once the meat’s are cooked walk to the dark X mark and collect() "salt" and "pepper" and put them in a list named ingredients .

player.collect("salt")
player.collect("pepper")
ingredients = ["salt","pepper"]

Afterwards create nested lists named served_pork , served_beef , served_lamb and served_buffalo and append the meat you cooked along with the ingredients list.

served_pork = []
served_pork.append("cooked pork")
served_pork.append(ingredients)

# Do the same for the other meats

Walk to the gold X marks on the upper X mark place() served_pork and served_buffalo and on the bottom X mark place() served_lamb and served_beef . With this you will have completed the level.

Map overview and path

Map overview and path Map overview and path

Code Solution

import player

# Set Parent Class

class cooked_meat:
  
  type = ""
  
  def cook_meat(self):
    player.speak("Cooking the %s meat with oil and onions." 
    % self.type)
    player.place("meat")
    player.collect("cooked %s" % self.type)

# Set Child Class  
  
class ready_meat(cooked_meat):
  rarity = ""
    
  def cook_meat(self):
    super().cook_meat()
    player.speak("The meat is cooked %s" % self.rarity)
    
player.turn_right()
player.move_forward(6)
player.turn_right()
player.move_forward()
player.turn_right()
player.move_forward()
player.turn_right()
player.move_forward(2)
player.turn_left()
player.move_forward()
player.turn_left()
player.move_forward()
player.turn_right()
player.move_forward(7)
player.turn_left()

pork_meat = ready_meat()
pork_meat.type = "pork"
pork_meat.rarity = "medium"
pork_meat.cook_meat()

beef_meat = ready_meat()
beef_meat.type = "beef"
beef_meat.rarity = "well done"
beef_meat.cook_meat()

buffalo_meat = ready_meat()
buffalo_meat.type = "buffalo"
buffalo_meat.rarity = "medium rare"
buffalo_meat.cook_meat()

lamb_meat = ready_meat()
lamb_meat.type = "lamb"
lamb_meat.rarity = "rare"
lamb_meat.cook_meat()

player.turn_right()
player.move_forward()
player.turn_left()
player.collect("salt")
player.collect("pepper")

ingredients = ["salt","pepper"]

served_pork = []
served_pork.append("cooked pork")
served_pork.append(ingredients)

served_beef = []
served_beef.append("cooked beef")
served_beef.append(ingredients)

served_buffalo = []
served_buffalo.append("cooked buffalo")
served_buffalo.append(ingredients)

served_lamb = []
served_lamb.append("cooked lamb")
served_lamb.append(ingredients)

player.turn_left()
player.turn_left()
player.move_forward(4)
player.turn_left()
player.move_forward()
player.turn_right()

player.place(served_lamb)
player.place(served_beef)

player.turn_left()
player.turn_left()
player.move_forward()

player.place(served_pork)
player.place(served_buffalo)
Chapter 5 / Level 3: Super Function | Teacher's Guide