列表理解皮加尔酒吧()x×U标签

2024-04-26 19:05:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我用pygal来模拟骰子滚动。我想把它另存为svg文件。你知道吗

import pygal
from die import Die


die_1 = Die()
die_2 = Die(10)
results = []

for roll_num in range(50000):
    result = die_1.roll() + die_2.roll()
    results.append(result)

frequencies = []
max_result = die_1.num_sides + die_2.num_sides

for value in range(2,max_result+1):
    frequency = results.count(value)
    frequencies.append(frequency)

hist = pygal.Bar()

hist.title = "Result of rolling a D6 and D10 50000 times"
hist.x_labels = [x for x in list(range(2,max_result+1))]
hist.x_title = "Result"
hist.y_title = "Frequency of Result"

hist.add('D6+D10', frequencies)
hist.render_to_file('different_dice.svg')

但它给出了一个错误:

if len(string) > index and index > 0: TypeError: object of type 'int' has no len()"

如果我对列表行进行注释

hist.x_labels = [x for x in list(range(2,max_result+1))]

会有用的。这张单子有什么不正确的地方吗?你知道吗


Tags: infortitlerangeresultresultsnumhist