### 03 Dice

Using lists, write a program that will roll a specified number of dice and add the rolls. The program must do this a specfied number of times and print a horizontal histogram showing the distribution of scores. The base of the histogram rows must have the score (e.g. 3, 4, 5, etc.). Each row of the histogram must be scaled so that the length of the maximum bar is 50 stars. The number of times that score was rolled must be printed at the end of the bar. For example, the output should look like this:

`>>> `
`number of dice? 2`
`number of rolls? 200`

`2 *************** 10`
`3 ************ 8`
`4 ************* 9`
`5 *********************************** 24`
`6 *************************************** 27`
`7 *************************************************** 35`
`8 ************************************************* 34`
`9 ************************** 18`
`10 **************************** 19`
`11 ****************** 12`
`12 ****** 4`

`Type 'done' to exit: `

`number of dice? 3`
`number of rolls? 2000`

`3 *** 12`
`4 ***** 24`
`5 ********** 48`
`6 ******************* 98`
`7 ********************** 116`
`8 ************************************** 198`
`9 ***************************************** 215`
`10 ********************************************** 240`
`11 *************************************************** 265`
`12 ********************************************** 242`
`13 *************************************** 203`
`14 *************************** 141`
`15 ******************* 96`
`16 ************* 65`
`17 ******* 35`
`18 * 2`

`Type 'done' to exit: done`
`>>> `

The random module will be useful for generating random numbers. Your code must comply with the style tips we discussed in class.

## Study Guide

Sample code for the Dice project is attached below.

C-Level Questions will require you to understand
1. Creating lists and adding elements to lists
2. Indexing into lists. For example, if L = [1,2,5,3,6], what does L[2] return?
3. Using for-loops and while-loops to access and modify list entries
C-level questions only require you to understand simple code fragments and write their output. You do not need to write any code for the C-level questions.

B- and A-Level Questions will require you understand
1. C-Level content
2. Error handling i.e. try-except clauses
3. len() command used with lists, e.g., if L = [2,3,4,3] you should know what  len(L) returns
4. list methods, such as [ ].count()
B- and A-level questions require you to not only understand code fragments and write their output, but also may ask you to write short code fragments from scratch that meet the question's requirements.

The coding test will require you to understand B and A-level content.

The Python Tutorials page is a great place to start for your studying.  There's also the unofficial textbook, How to Think Like a Computer Scientist.

ċ
03 dice.sample.py
(2k)
Nikhil Joshi,
May 19, 2013, 9:34 AM