Teacher's Guide

Chapter 2 / Level 2: Variant Argument Functions

Teaching Objective:

  • Student learns how to further customize custom function arguments
  • Understands how to use lists for adding arguments on a function
  • Learns how to use dictionaries as arguments and greatly expand a function’s accessible data.

Guide

In this exercise we’re going to be working with variant argument functions, these are custom functions that can have a variable number of argument functions, meaning the number of arguments the function has is defined by you. This is done by either setting the arguments in the custom function as a list or arguments, or by setting them as a dictionary of arguments.

The objective for this level is to collect and pack all the food items in the storehouse for shipment. First set the custom functions for packing single items and for packing multiple items. By setting the arguments with a * in front of them, you place them in an argument list, and by setting ** in front of them you place them in an argument dictionary.

# Argument list function

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)

# Argument dictionary function

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)

Walk into the storehouse and grab: "jars" and "eggs" which are packed as singles, and grab: "berries" and "fruit" which are packed as multiples. The difference between these is that the items that are packed as singles have one unique version of each, whereas the items packed in multiples have several of each item.

Once all times in the storehouse have been collected, walk to the dark X mark on the green carpet and use the pack_singles() function to pack the jars of each color. The first argument is the type which is "jars" and the rest are strings of the different colors of jars you’ve collected.

pack_singles("jars", "white", "purple", "red", "brown")

Walk to the dark X mark on the orange carpet and use the pack_singles() function to pack the eggs of each color. The first argument is the type which is "eggs" and the rest are strings of the different colors of eggs you’ve collected.

pack_singles("eggs", "white", "green", "blue")

Next walk to the light X mark on the blue carpet and use the pack_multi() function to store the berries of each type you’ve collected. The first argument is the type which is "berries" and the rest are the various berries you’ve collected followed by the amounts you’ve collected of each.

pack_multi("berries", red = # , blue = # , purple = # )

Next walk to the light X mark on the red carpet and use the pack_multi() function to store the fruit of each type you’ve collected. The first argument is the type which is "fruit" and the rest are the various fruits you’ve collected followed by the amounts you’ve collected of each.

pack_multi("fruit", apple = # , pear = # , orange = # , peach = #)

Once all items are packed you will have completed the level.

Map overview and path

Map overview and path

Code Solution

import player

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)
  
player.turn_right()
player.move_forward(2)
player.turn_right()
player.move_forward(5)
player.turn_left()
player.move_forward(2)
player.turn_left()
player.move_forward(3)
player.turn_right()
player.move_forward()
player.turn_right()
player.move_forward(3)
player.turn_left()
player.move_forward(2)
player.turn_left()
player.move_forward(3)
player.turn_right()
player.move_forward()
player.turn_right()
player.move_forward(3)
player.turn_left()
player.move_forward(2)
player.turn_left()
player.move_forward(3)
player.turn_left()
player.move_forward(6)
player.turn_right()
player.move_forward(4)

pack_singles("jars", "white", "purple", "red", "brown")

player.turn_right()
player.move_forward(7)
player.turn_left()

pack_singles("eggs", "white", "green", "blue")

player.turn_left()
player.move_forward(2)
player.turn_right()
player.move_forward()

pack_multi("berries", red = 3 , blue = 2 , purple = 4 )

player.turn_left()
player.move_forward(2)
player.turn_right()

pack_multi("fruit", apple = 4 , pear = 3 , orange = 2 , peach = 2)

Chapter 2 / Level 2: Variant Argument Functions | Teacher's Guide