11. Yahtzee

Studios are in-class activities to give you hands-on practice with new concepts. The first half is the Walkthrough, an instructor-led programming problem. The second half is for you to work on individually or in pairs in class.

These problems are not graded, and you are not obligated to finish them. Get as far as you can while in class, and use them as an opportunity to play with and solidify new concepts.


Imagine that you work for a large retailer, writing software for its internal accounting system. You’ve been asked to write a function average_sales that can take a set of data representing daily sales at a given store, and calculate the average sales for each week represented by the data.

The data is structured as follows:

A single week of daily sales is a list with 7 entries, one for each day of the week (index 0 = Monday, index 6 = Sunday): [1512.30, 1555.72, 1989.77, 2101.33, 1884.45, 1333.33, 1456.23]

You are given data for several weeks at once, collected into a list:

sales = [[1512.30, 1555.72, 1989.77, 2101.33, 1884.45, 1333.33, 1456.23],
[1215.340, 1565.99, 2989.34, 2301.77, 1234.81, 1923.44, 2282.39],

Therefore, sales is a list of lists. So here, sales[0] is a list of sales totals for each day of the first week, sales[1] a list of sales totals for each day of the second week, and so on. We will implement average_sales so that it returns a list of the averages for each week.

So if sales is the list above and we call weekly_averages = average_sales(sales), then weekly_averages[0] is the average for week 0, and so on.


In this studio, we write some functions that could be used in a simulation of Yahtzee. The program asks the user for the number of die they roll and the number of times the dice are thrown. The roll_dice function is given to you. You must create the sum_of_roll function. If you finish that, you move on to the bonus function, yahtzee.