我正在编写这个非常简单的代码来学习Python。我的目标是,获得当我多次掷两个骰子时可能出现的二项分布图。为此,我到目前为止编写了这些代码行:
import random
import numpy
class Dice:
def roll(self):
first = random.randint(1, 6)
return first
class Dice2:
def roll(self):
second = random.randint(1, 6)
return second
storage1 = []
storage2 = []
for rolling in range(10, 0, -1):
dice = Dice()
storage1.append(dice.roll())
storage2.append(dice.roll())
list1 = numpy.array((storage1)) + numpy.array((storage2))
print(list1)
x = 5
count = 0
for dice in list1:
if(dice == x):
count = count + 1
print(count1)
所以我在这里想做的是输出一个元素的计数,在本例中 x = 5,换句话说,当我掷 2 个骰子时,我会抛出 5 多少次。尤其是最后一部分:
list1 = numpy.array((storage1)) + numpy.array((storage2))
print(list1)
x = 5
count = 0
for dice in list1:
if(dice == x):
count = count + 1
print(count1)
似乎不起作用,输出是我不明白的东西,它输出如下:
[ 2 7 7 8 7 4 7 9 10 10]
#This is the output from the line: list1 = numpy.array((storage1)) + numpy.array((storage2))
# so this part I understand, it is the addition of dice1 and dice2, like wished
1
1
1
1
1
1
1
1
1
1
# This is the Output from the loop and finally the print(count1)
我想知道,如何存储出现次数,从 2 到 12 的任何数字(来自掷两个骰子)确实会出现。
慕斯709654