Solutions

Use assert to catch divide by zero

def mean(sample):
    '''
    Takes a list of numbers, sample

    and returns the mean.
    '''

    assert len(sample) != 0
    sample_mean = sum(sample) / len(sample)
    return sample_mean

We can further improve the function by adding an error message to our assert statement:

def mean(sample):
    '''
    Takes a list of numbers, sample

    and returns the mean.
    '''

    assert len(sample) != 0, "Unable to take the mean of an empty list."
    sample_mean = sum(sample) / len(sample)
    return sample_mean

Run each of these functions to identify where the error message is displayed.

Checking type

def mean(sample):
    '''
    Takes a list of numbers, sample

    and returns the mean.
    '''

    assert len(sample) != 0, "Unable to take the mean of an empty list"
    for value in sample:
        assert isinstance(value,int) or isinstance(value,float), "Value in list is not a number."
    sample_mean = sum(sample) / len(sample)
    return sample_mean

We have now test whether each value in the list is a number. Check that this new function performs as you would expect for your lists.