[Python Crash Course] DataVisual_RollDice

from random import randint


class Die:
    """ represent a single die """

    def __init__(self, num_side=6):         # D6: a die with 6 sides
        self.num_side = num_side

    def roll(self):
        return randint(1, self.num_side)    # randint[min,max]: return the random number representing the side


die = Die()

# the results of rolling #
results = []
for roll_num in range(100):
    result = die.roll()
    results.append(result)
print(results)

# the frequency of each number #
frequencies = []
for value in range(1, die.num_side+1):      # range[min,max)
    frequency = results.count(value)
    frequencies.append(frequency)
print(frequencies)

import pygal

hist = pygal.Bar()

hist.title = "Result of rolling D6"
hist.x_labels = ['1', '2', '3', '4', '5', '6']
hist.x_title = "Result"
hist.y_title = "Frequency"

hist.add('D6', frequencies)
hist.render_to_file('die_visual.svg')       # open with browser

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页