Python Development Course
Chapter
>
Level

Creating your own Functions
Using Operators in Functions

Objective

Collect all the berries and check if you have enough to mash by using operators in custom functions.

There are several berries in the field that you could collect and mash to form a puree however you need to see if you have enough berries for a serving. To facilitate this, use a custom function to have it run a number calculation for you.

First thing we must do is set up a variable named serving, this is the amount needed for a portion of food, this variable should be the total amount of berries in the field divided (/) by three (3), like this serving = total amount of berries / 3 .

You must count the total amount of berries in the field yourself and enter the number. The reason it’s divided by three (3) is because there are three (3) types of berries in the field: red berries , blue berries and black berries. The value of the serving variable is the average number of berries in the field.

Set up a custom function for future use named mash_berries , this function checks if you have enough berries of the variable you input in it and returns a message.

def mash_berries(item): if item > serving: player.speak("You have too many berries") player.speak("You have %d more than needed" % (item - serving) ) if item == serving: player.speak("You have the right amount of berries") if item < serving: player.speak("You have too few berries") player.speak("You need %d more for a serving" % ( serving - item) )

Collect all the berries in the field, and store the number you collect of each type in the variables: red_berries , blue_berries and black_berries which are the purple berries present in the field. Head for their corresponding X marks and use the custom function entering each variable as an argument, for example: mash_berries(red_berries) .

Complete the level by doing this with all three berry types, the sings next to the X mark telling you which berry should be checked at that location. The top X mark being black berries and the bottom being blue berries.

Code book