用python模拟2个六面骰子滚动的总和

用python模拟2个六面骰子滚动的总和,python,simulator,python-3.2,Python,Simulator,Python 3.2,写一个模拟滚动2个六面骰子的程序。你的程序应该有一个函数Roll(),它返回掷骰子的总数。您可以假设六个面中的每一个都有相同的可能被掷骰子(也就是说,骰子是“公平的”)。运行模拟1000次,并报告每次求和的频率 到目前为止,我已经有了这些,但我的程序似乎无法计算出总数。我可能完全错了。请帮忙。我想我的主要问题是打印对账单。我需要的输出打印多少次的总和2显示,总和3,总和4,等12 def Roll(): for i in range(1000): one = 0

写一个模拟滚动2个六面骰子的程序。你的程序应该有一个函数Roll(),它返回掷骰子的总数。您可以假设六个面中的每一个都有相同的可能被掷骰子(也就是说,骰子是“公平的”)。运行模拟1000次,并报告每次求和的频率

到目前为止,我已经有了这些,但我的程序似乎无法计算出总数。我可能完全错了。请帮忙。我想我的主要问题是打印对账单。我需要的输出打印多少次的总和2显示,总和3,总和4,等12

def Roll():
    for i in range(1000):
        one = 0
        two = 0
        three = 0
        four = 0
        five = 0
        six = 0
        dice1= float(0)
        dice2= float(0)

        dice1 = random.randint(1,6)
        if dice1 == 1:    
            one = one + 1
            count= 1
            return count
        elif dice1 == 2:
            two = two + 1
            count= 1
            return count
        elif dice1 == 3:
            three = three + 1
            count= 1
            return count
        elif dice1 == 4:
            four = four + 1
            count= 1
            return count
        elif dice1 == 5:
            five = five + 1
            count= 1
            return count
        else:
            six = six + 1
            count= 1
            return count

        dice2 = random.randint(1,6)
        if dice2 == 1:    
            one = one + 1
        elif dice2 == 2:
            two = two + 1
        elif dice2 == 3:
            three = three + 1
        elif dice2 == 4:
            four = four + 1
        elif dice2 == 5:
            five = five + 1
        else:
            six = six + 1

        total = one + two + three + four + five + six

        print("2", dice1 + dice2)   
        print("3", dice1 + dice2)
        print("4", dice1 + dice2)
        print("5", dice1 + dice2)
        print("6", dice1 + dice2)    
        print("7", dice1 + dice2)
        print("8", dice1 + dice2)   
        print("9", dice1 + dice2)
        print("10", dice1 + dice2)
        print("11", dice1 + dice2)
        print("12", dice1 + dice2)

我已经用同样的任务回答了你的一位朋友:

import random
from collections import defaultdict

def main():
    dice = int(input("Enter the number of dice: "))
    sides = int(input("Enter the number of sides: "))
    rolls = int(input("Enter the number of rolls to simulate: "))
    result = roll(dice, sides, rolls)
    maxH = 0
    for i in range(dice, dice * sides + 1):
        if result[i] / rolls > maxH: maxH = result[i] / rolls
    for i in range(dice, dice * sides + 1):
        print('{:2d}{:10d}{:8.2%} {}'.format(i, result[i], result[i] / rolls, '#' * int(result[i] / rolls / maxH * 40)))


def roll(dice, sides, rolls):
    d = defaultdict(int)
    for _ in range(rolls):
        d[sum(random.randint(1, sides) for _ in range(dice))] += 1
    return d

main()
你可以取出对你有用的零件。这还应包括后续问题,如:“如何将其打印整齐”和“如何绘制直方图”


例如:

Enter the number of dice: 2
Enter the number of sides: 6
Enter the number of rolls to simulate: 1000
 2        28   2.80% ######
 3        59   5.90% #############
 4        84   8.40% ###################
 5        96   9.60% ######################
 6       155  15.50% ####################################
 7       170  17.00% ########################################
 8       147  14.70% ##################################
 9       102  10.20% #######################
10        80   8.00% ##################
11        50   5.00% ###########
12        29   2.90% ######

这里有一个快速而肮脏的方法

from collections import Counter
import random

def roll():
  return random.randint(1,6) + random.randint(1,6)

counter = Counter( roll() for _ in range(1000) )

for num, cnt in counter.iteritems():
  print '%2d: %.3f' % (num, cnt / 1000.0)
导致

 2: 0.022
 3: 0.063
 4: 0.072
 5: 0.104
 6: 0.154
 7: 0.174
 8: 0.141
 9: 0.112
10: 0.077
11: 0.057
12: 0.024

我删除了SQL标签,因为这似乎与SQL无关。您的回答非常有用,我非常感谢。我只改变了第一部分。我不想让程序询问用户掷了多少骰子,所以我分配了骰子、边和骰子的数量。我还有一个问题。我不需要打印百分比部分。我试着重新安排,但它给了我一个错误。我怎样才能解决这个问题。换句话说,我只想要输出中的前两个“列”?用
{:2d}{:10d}{:8.2%}{}
替换为
{:2d}{:10d}
哦,我明白了。我保留了{}并且刚刚删除了{:8.2%}…你能解释一下为什么{}也被删除了吗?