高级骰子滚动模拟器与python中的直方图

时间:2013-12-02 21:41:45

标签: python python-3.x histogram dice

我正在编写一个程序,询问用户骰子的数量以及骰子上的边数。它计算每个值被滚动的次数,然后将它们放入列表中。然后我必须打印列表以及百分比和值的直方图,并且直方图的最大值为80,所有内容都基于此。我不知道为什么,但我很难过。到目前为止,这是我的代码:

import random

def histogram(L):
    mx = max(L)
    scale = 80/mx
    for num in L:
        print("#"*int(num*scale))

def main():
    rolls = int(input("How many times do you want to roll the dice?"))
    sides = int(input("How many sides on the dice?"))
    L = []
    for i in range(rolls+1):
        L.append(random.randrange(1,sides+1))

    histogram(L)

main()

1 个答案:

答案 0 :(得分:0)

Unlimited sides to dice in simulator

开始

然后

def bar(value, scale):
    print('{} {}'.format('#'*int(value*scale + 0.5), value))

def histogram(results, maxbar=76, displayfn=bar):
    # find the histogram domain
    values = list(results.keys())
    lo, hi = min(values), max(values)
    # find the data range
    scale = maxbar / float(max(results.values()))
    # output the results
    for val in rng(lo, hi+1):
        bar(results[val], scale)