我正在使用 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')
但它给出了一个错误:
如果 len(string) > index 和 index > 0: TypeError: 'int' 类型的对象没有 len()"
如果我评论列表理解行
hist.x_labels = [x for x in list(range(2,max_result+1))]
它会起作用的。这个列表理解有什么不正确的吗?