Coding for KidsCoding for Kids
Creative LevelsChallengesTeacher's Guide
Vote for features
Advanced Python Development Course
Chapter
>
Level

Advanced Classes
Method Overriding

Objective

Bottle oil in order to cook meat by creating objects with overridden methods.

There’s some oil that you could use to cook some vegetables, it needs to be bottled first for ease of use. Using classes and creating objects would be an effective method of accomplishing this, much like in the previous level. That said there are also ways to more effectively use object inheritance within a class, such as having the child class override functions in the parent class.

# Parent Class class oil_bottle: label = "" async def label_bottle(self): await player.read() # Child Class class fill_bottle(oil_bottle): def fill_bottle(self): player.place("empty bottle") player.collect("oilbottle") async def label_bottle(self): await player.write(self.label)

In the above code the child class overrides the function label_bottle() on the parent class to rewrite the labels on the bottles. This also allows you to set a custom message using async functions.

To start off, grab all four (4) of the empty bottles on the field, once collected walk to dark X marks and create objects using the classes.

There are four (4) types of oil to be bottled, they correspond to the colored carpets in front of the machines: olive_oil is bottled on the yellow carpet, avocado_oil is bottled on the green carpet, canola_oil is bottled on the blue carpet, and vegetable_oil is bottled on the red carpet.

Create objects for each of the oils, write their label and activate the child class functions label_bottle() and fill_bottle() , for example:

olive_oil = fill_bottle() olive_oil.label = "Olive Oil" olive_oil.label_bottle() olive_oil.fill_bottle()

Each of the labels should contain the capitalized name of each of the oils as follows: "Olive Oil" , "Avocado Oil" , "Canola Oil" and "Vegetable Oil" .

Once you bottle the oils, walk to the light X mark in front of a sack and use the collect() function to grab "onion" from the sack. Afterwards walk to the gold X mark and use the oil to cook the onion by storing them together on a list named ingredients, like this: ingredients = ["Olive Oil", "onion"] . Use the place() function with the list to cook the ingredients.

Finally place the remaining oils on a list named oils , like this: oils = ["Avocado Oil","Canola Oil","Vegetable Oil"] . Walk to the light X mark on the purple carpet, face the cabinet and use the place() function with the oil list to store the items in the cabinet and complete the level.

Code book