今天看了pygal,自己学了学,录了个视频。
import pygal
from die import Die
die_1 = Die()
die_2 = Die(10)
results = []
for roll_num in range(10000):
result = die_1.roll() + die_2.roll()
results.append(result)
frequencies = []
for value in range(2, 17):
frequency = results.count(value)
frequencies.append(frequency)
hist = pygal.Bar()
hist._title = "roll"
hist.x_labels = ['2', '3', '4', '5', '6', '7','8', '9', '10', '11', '12','13', '14', '15', '16']
hist._y_title = "Frequncy of Result"
hist.add('D6 + D6', frequencies)
hist.render_to_file('die_visual.svg')
import random
class Die():
def __init__(self, num_sides = 6) -> None:
self.num_sides = num_sides
def roll(self):
return random.randint(1,self.num_sides)