GGR274 Lab 8: Functions#

Logistics#

Like previous weeks, our lab grade will be based on attendance and submission of a few small tasks to MarkUs during the lab session (or by 23:59 on Thursday).

Complete the tasks in this Jupyter notebook and submit your completed file to MarkUs. Here are the instructions for submitting to MarkUs (same as last week):

  1. Download this file (Lab_8.ipynb) from JupyterHub. (See our JupyterHub Guide for detailed instructions.)

  2. Submit this file to MarkUs under the lab8 assignment. (See our MarkUs Guide for detailed instructions.)

Lab Instructions and Learning Objectives#

  • Writing functions that take arguments

  • Calling functions with keyword arguments

Task 1 - Write a simple function#

Write a function called my_mean() to compute the average of a list of numbers.

  • What should the argument (or arguments) to my_mean() be?

  • Challenge yourself to write the loop that computes the average rather than using the built-in mean()function

# Write your code here

Call your function with the list heights and store the result in mean_result.

heights = [172, 190, 175, 183]

# Write your code here


# check your work
print(mean_result)

Task 2 - Rewrite code from Lab 7 as a function#

In last week’s lab, you wrote some code to try rolling a six-sided die multiple times. Your task today is to encapsulate the experiment in function, so that we can try different parameters.

Here is the solution code from last week.

import pandas as pd

die_size = 6
die_list = list(range(1, die_size + 1))

die_series = pd.Series(die_list)

results = []

for i in range(10000):
    roll = die_series.sample(n = 10, replace = True, random_state = i)
    roll_sum = sum(roll)
    results.append(roll_sum)

results = pd.Series(results)

results

Now suppose we want to rerun this type of experiment multiple times with different values for the size of the die, different number of rolls for each experiment, and different number of experiments. These 3 variables would make great arguments to a function.

Your task is to take the code above and write a function called simulate_dice_rolls() that takes three arguments:

  • num_experiments an integer that determines how many experiments. In the code above this is 10000

  • num_rolls this is the number of rolls of the dice that are summed an added to the results list

  • die_size the highest number the die can roll

The return value of the fuction is results a Series representing the output of each experiment. Remember that you can create a Series from a list using pd.Series(list_variable)

# Write your code below

Call your new function with the following arguments and store the result in a variable called sim_result

Arguments:

  • num_experiments = 100

  • num_rolls = 2

  • die_size = 10

# Write your code here




#Check your work
print(sim_result)

We will plot the result of this experiment. What do you notice?

results_count_sorted = sim_result.value_counts().sort_index()

results_count_sorted.plot.bar()

Try running your function with different arguments to see how large num_experiment and num_rolls has to get before the graph looks more like a normal distribution. (Not markeds)

Remember to use a different variable to store the return value so that sim_result has the correct value above.

# Write your code here
result = simulate_dice_rolls(100, 10, 10)
results_to_plot = result.value_counts().sort_index()
results_to_plot.plot.bar()