CL3: Review (Collections, Conditionals, & Functions)#

Welcome to the third coding lab!

In this Coding Lab we will return to, review, and get additional practice with collections, conditionals, and functions working to put all the concepts together.

Part 1: Collections + Conditionals#

Collections can work with all of the things we’ve discussed up to this point - variables, conditionals, functions, etc. Here, we’ll get extra practice combining collections with conditionals.

Collections + Conditionals Question#

Imagine you’ve asked a group of young children to make a pile of at least 5 toys.

You then want to write code to determine if the kid accomplished the task. Specifically, if the pile has at least 5 toys, you want output to store the string 'success'. Otherwise, you want output to store the string 'try again'

The toys placed in one of these kids’ piles is defined below in the list kid_a. Using this list, and conditionals, write code to accomplish the task above!

kid_a = ['truck', 'barbie', 'coloring book', 'dinosaur']
### BEGIN SOLUTION
if len(kid_a) >= 5:
    output = 'success'
else:
    output = 'try again'
### END SOLUTION
assert output in ['success', 'try again']

Part 2: Collections + Conditionals + Functions#

Let’s put these conepts together…!

Collections + Conditionals + Functions Question#

The code you generated in Part II will only work on a single list kid_a. Functions, on the other hand, are ways to define a set of instructions that can operate on various input, returning the correct output.

Here, write a function toy_pile() that accomplishes the same task as above, but takes a list of toys as input and returns either 'success' or 'try again', depending upon how many toys are in the list of toys.

### BEGIN SOLUTION
def toy_pile(list_of_toys):
    
    if len(list_of_toys) >= 5:
        output = 'success'
    else:
        output = 'try again'
    
    return output
### END SOLUTION
# test out your function here 
assert toy_pile(kid_a) == 'try again'
assert toy_pile(['lego', 'princess dress', 'tinker toy', 'doll', 'stroller']) == 'success'

Putting it all together#

Now that you’ve seen an example, think up another situation where you could write a function that incorporates the following:

  • takes a dictionary as input

  • utilizes a conditional within the function

  • carries out some operation and returns some output

First, explain what your function does in plain English in the cell below. Then, write the python code for the function in the following cell and test it out in the last cell.

Explain your function here (in words, not code)

# define your function here
### BEGIN SOLUTION

# answers will vary here

### END SOLUTION
# test out/execute your function here

Part 3: Debugging#

Debugging a Function#

The following function has a series of issues with it.

def my_function(input_1, input_2):
    """A long function that might error."""
    
    if len(input_1) > 1
        if input_1[0] = 0:
        answer = 0
    
    elif len(input_2) == 2:
        answer = input_2[1] / input_1[0]
        
    elif len(input_1) == len(input_2):
        answer = input_1[0] + input_2[0]
        
    print(answer)

Copy this function into the cell below, and then work through debugging this function, fixing it up to be able to run with all the inputs below.

### BEGIN SOLUTION
def my_function(input_1, input_2):
    """A long function that might error."""
    
    if len(input_1) > 1:
        if input_1[0] == 0:
            answer = 0
    
    elif len(input_2) == 2:
        try:
            answer = input_2[1] / input_1[0]
        except ZeroDivisionError:
            answer = None
        
    elif len(input_1) == len(input_2):
        try:
            answer = input_1[0] + input_2[0]
        except TypeError:
            answer = None
        
    return answer 
### END SOLUTION
my_function([0, 1], [0])
0
my_function([0], [0, 1])
my_function([1], [1])
2
my_function([1], ['1'])

The End!#

This is the end of this Coding Lab!

Be sure you’ve made a concerted effort to complete all the tasks specified in this lab. Then, go ahead and submit on datahub!