Python Development Course
Chapter
>
Level

Creating your own Functions
Passing functions as arguments

Objective

Count the mandrakes and mushrooms in the swamp using functions.

Count the various strange objects located in the swamp, using custom functions to make it easier to format your code. Walk to the X marks and count, how many “Red Mandrake” , “Blue Mandrake” , “Poison Mushroom” and then count all of them together at the light X mark.

Custom functions are capable of returning values, these values can also be used as arguments in an existing functions.

def name_and_number(name, number): return( "There are %d %ss in the field." % (number, name))

By placing return() at the end of the function the function itself will return a value, in this case a string made with the arguments you entered. Use the value from this function to call out the name and number of each object using the speak() function for example: player.speak(name_and_number("Red Mandrake", 4))

def add_numbers(num1, num2, num3): return(num1 + num2 + num3)

For the final light X mark create another custom function to add the numbers together and return the total of items you counted.

Code book