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

Advanced Functions
Variant Argument Functions

Objective

Grab all the consumables inside the storehouse and pack them in storage containers using variant argument functions.

There are several food items located inside a storehouse that need to be packed in order to be shipped out to be sold. In order to efficiently do this, you can create custom functions with a variable number of arguments. These are functions, that have no set number of arguments and allow you to set as many arguments you require to execute the function’s actions.

There are two (2) ways to add a variable number of arguments to a function, depending on what you’re trying to achieve. The first method is creating an argument list, instead of putting various argument names, place an asterisk in front of a single argument. This automatically creates a list.

def custom_function(message,*arguments): player.speak(message) return(arguments) value = custom_function("lalala","one","two","three") # The message "lalala" is printed on screen # Value now returns the list ("one","two","three")

The second method is placing the arguments inside a dictionary instead of the list, this is done in the same way as in the previous method but instead of putting one asterisk, you put two. This automatically creates a dictionary instead of a list.

def custom_function(message,**arguments): player.speak(message) return(arguments) value = custom_function("lalala", one = 1, two = 2, three = 3) # The message "lalala" is printed on screen # Value now returns the dictionary: # {'one': 1, 'three': 3, 'two': 2}

To pack all the items for shipment, create two sets of custom functions, one for packing single items and one for packing multiple items together. Use the variable arguments to pack together various items together without needing to make too many functions.

def pack_singles(name,*items): length = len(items) player.speak("There are %d types of %s, those being:" % (length, name) ) player.speak(items) player.place(items) def pack_multi(name,**items): length = len(items) player.speak("There are %d types of %s, those being:" % (length, name) ) player.speak(items) player.place(items)

There are four (4) different types items on the field: "jars" , "eggs" , "berries" and "fruit". Collect all items in the storehouse and walk towards the X marks on the shipping lane. Walk to the dark X marks and use the pack_singles functions to pack the "jars" and "eggs".

For the "jars" there are four(4) types you collect and are entered as arguments, those being "white", "purple", "red" and "brown" . These are to be stored in the container by the green carpet. The code being written like this: pack_singles("jars", "white", "purple", "red", "brown")

For the "eggs" there are three (3) types you collect and are entered as arguments, those being "white", "green", and "blue" . These are to be stored in the container by the orange carpet. The same format is used as in the previous setup.

Afterwards walk to the light X marks and use the pack_multi function to pack the "berries" and "fruit" . For the "berries" there are three (3) types you collect and are entered as arguments, those being "red", "blue", and "purple" . These are to be stored in the container by the blue carpet. You need to add the quantities you collect of each for these, like this: pack_multi("berries", red = 3 , blue = 2 , purple = 4 )

For the "fruit" there are four (4) types you collect and are entered as arguments, those being "apple", "pear", "orange", and "peach" . These are to be stored in the container by the red carpet. Use the same format as in the previous entry, make sure all items are collected and stored in order to complete the level.

Code book