Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将骰子试验和柱状图相加_Python - Fatal编程技术网

Python 将骰子试验和柱状图相加

Python 将骰子试验和柱状图相加,python,Python,我被困在python中的一段代码中,该代码接受骰子数和掷骰子数,并返回获得的数字之和。它还应该打印总和的直方图。我被困在代码的第一部分。有人能帮我修一下吗?我不知道我会错在哪里。对第二部分(返回直方图)的任何帮助都将有助于我在python中学习它 from random import choice def roll(rolls,dice): d = [] for _ in range(rolls): d[sum(choice(range(1,7)) for _

我被困在python中的一段代码中,该代码接受骰子数和掷骰子数,并返回获得的数字之和。它还应该打印总和的直方图。我被困在代码的第一部分。有人能帮我修一下吗?我不知道我会错在哪里。对第二部分(返回直方图)的任何帮助都将有助于我在python中学习它

from random import choice

def roll(rolls,dice):
    d = []
    for _ in range(rolls):
        d[sum(choice(range(1,7)) for _ in range(dice))] += 1

return(d)

这里的问题是您不能随意索引到空列表中:

l = []
l[13] += 1 # fails with IndexError
相反,您可以使用a,这是一种特殊类型的字典,它不介意是否尚未使用密钥:

from collections import defaultdict
d = defaultdict(int) # default to integer (0)
d[13] += 1 # works fine, adds 1 to the default
或者,它专为此类情况设计(“提供以支持方便和快速的计数”),并提供额外方便的功能(如
最常见(n)
,以获取
n
最常见的条目):

要手动使用标准的
dict
执行此操作,只需添加一个检查:

d = {}
if 13 in d: # already there
    d[13] += 1 # increment
else: # not already there
    d[13] = 1 # create
试试这个

from random import choice
import pylab

def roll( rolls, dice ):
s = list()
for d in range( dice ):
    for r in range( rolls ):
        s.append( choice( range(1,7) ) )
return s

s = roll( rolls, dice )

sum_of_rolls = sum( s )

# then to plot..
pylab.hist( s )
这应该可以

import random

def rolls(N, r):  # N=number of dice. r=number of rolls
    myDie = [1,2,3,4,5,6]
    answer = {}
    for _rolling in range(r):
        rolls = []
        for _die in range(N):
            rolls.append(random.choice(myDie))
        total = 0
        for roll in rolls:
            total += roll
        if total not in answer:
            answer[total] = 0
        answer[total] += 1
    return answer

你可以看看这本书(它是免费的),它使用Python和许多骰子示例来教授贝叶斯统计。@cjohnson318:你的评论中没有链接。看起来你想在那里放一个。谢谢,这里是链接:你的
返回值
超出了你的功能范围。这不会让你生成直方图,你只能得到总数。对不起,我草率地浏览了一下。谢谢。我忘了提。应该在不使用内置函数(如defaultdict、counter等)的情况下完成。。只需使用简单的循环即可。谢谢。我忘了提。应该在不使用内置函数(如defaultdict、counter等)的情况下完成。。循环的使用非常简单。如果不允许使用
collections.defaultdict()
,则可以使用
setdefault()
方法执行类似操作。在这种情况下,可以使用标准
dict
执行此操作,并检查键-editedhanks。当我运行此命令时,我得到以下输出:回溯(最近一次调用last):文件“”,第1行,在rolls(2,2)文件“C:\Python33\dice.py”中,第8行,在rolls rolls.append(random.choice(1,7))中,类型错误:choice()接受2个位置参数,但3个是given@Rachel:修正!对不起
import random

def rolls(N, r):  # N=number of dice. r=number of rolls
    myDie = [1,2,3,4,5,6]
    answer = {}
    for _rolling in range(r):
        rolls = []
        for _die in range(N):
            rolls.append(random.choice(myDie))
        total = 0
        for roll in rolls:
            total += roll
        if total not in answer:
            answer[total] = 0
        answer[total] += 1
    return answer