Description
M5 Assignment 2 uses both modules from M5-1 and asks you to modify the second module to increase the number of rolls to 1000, to calculate how frequently each number appears, and to display the results in a bar char, which is called a histogram in Pygal.
Some of the code is written for you, so review the two files for module 1 in M5Lab2dice.py and module 2 in M5Lab2dice_hist.py file to see how the lab works. Some of the code is missing in M5Lab2dice_hist.py.
Read both program files to see what is missing. You will revise your pseudocode and revise the M5Lab2dice_hist.py file.
Pseudocode – Revise module 2 in your pseudocode to add these steps:
- Roll the dice 1000 times
- Do not print the results
- Instead, print the frequencies from a list called frequencies[]
- Frequencies are the number of times each number appears
- Add the titles to your bar chart
- # They are string values that appear at the top
- # List the titles for the x axis and the y axis of your chart
- # See the example in the code to support your pseudocode
- Render the chart to appear in a “dice_barchart.svg” file
- Add your name to the chart’s title
- Test your chart by dragging the file dice_barchart.svg into any Web browser window
- If you see your bar chart with 6 columns, examine it
- Hover your cursor over each column to see the values for 1000 dice rolls
- Save your pseudocode as M5Lab2ii.docx
Python code – Revise module 2 in M5Lab2dice_hist.py to add these steps:
- import Pygal # Pygal draws the bar chart or histogram for you
- import the Dice class # from dice import Dice
- declare the variable dice and assign the value to the class name Dice()
- # variable_name = Class()
- Declare an empty list called results
- Declare an empty frequencies list for the tracking how often each die value appears
- Declare a variable frequency and assign it to results.count(value)
- # Tracks the number of times each value from 1-6 is rolled
- print the frequencies # the format is print(listname)
- Bar chart: declare a variable hist and assign it the value pygal.Bar()
- Declare a hist.title variable and set the value to the following
- Your Name – Results of 1000 6-Sided Dice Rolls
- Replace “Your Name” with your name
- Declare a hist.x_title and set the value to Result using a string
- Declare a hist.y_title and set the string value to “How Often Did We Roll a 1, 2, 3, 4, 5, or 6?”
- # Tip: do not copy the string values surrounded by quotes from A MS Word file.
- # They use the wrong quotation symbols. Retype the quotation marks in Python.
- Save your M5Lab2dice_hist.py file into the same folder as dice.py
- # Both program files must be in the same folder
- Run your M5Lab2dice_hist.py file and see if it lists in the Python shell the number of times each number appeared on the dice rolls.
- Open the folder with your labs in it
- The results and frequencies from 1000 rolls appear in a hist.render_to_file called “dice_barchart.svg”
- Drag the dice_barchart.svg file from your folder to a Web browser window to see your bar chart
- Hover over the columns to see the frequencies appear