Teacher's Guide

Chapter 9 / Level 4: Class Functions

Teaching Objective:

  • Student understands how to set up custom functions in classes
  • Learns how to set up arguments to activate functions within a class object
  • Understands practical applications for class objects with custom functions

Guide

This level illustrates the usability and execution of functions within class objects. In this exercise you must activate pumps in order to refill water and food for the cows within the stable.

class machine:

   def refill(self, resource ):
     if resource == "food":
       player.place(resource)
       player.speak(food_amount)
     if resource == "water":
       player.water()
       player.speak(water_amount)

The machine that powers the pumps has a class named machine and it has a function titled refill() which can take the arguments "food" or "water" depending on what you wish to refill.

First create a class object reference so you may call the function.

pump = machine()

Walk to the four X marks on the field, On the dark X marks, reference the object and call the function setting it to refill food.

pump.refill("food") # On dark X marks

On the Light X marks, reference the object, call the function and set it to refill water.

pump.refill("water") # On light X marks

Use the class object’s function on all four X marks in order to complete the level.

Map overview and path

Map overview and path

Code Solution

import player

class machine:
	
   def refill(self, resource ):
     if resource == "food":
       player.place(resource)
       player.speak(food_amount)
     if resource == "water":
       player.water()
       player.speak(water_amount)

pump = machine()

player.move_forward()
player.turn_left()
player.move_forward(4)
player.turn_left()
pump.refill("food")
player.turn_right()
player.move_forward(2)
player.turn_left()
pump.refill("water")
player.turn_right()
player.move_forward(3)
player.turn_left()
player.move_forward(8)
player.turn_left()
player.move_forward(3)
player.turn_left()
pump.refill("food")
player.turn_right()
player.move_forward(2)
player.turn_left()
pump.refill("water")
Chapter 9 / Level 4: Class Functions | Teacher's Guide