滚动两个骰子并制表结果

时间:2014-05-11 23:18:09

标签: python random

问题在于:

你将编写一个程序来模拟一对骰子的滚动。

  • 您将询问用户要模拟的卷数。然后,您将每卷掷两个骰子。在每个骰子中使用随机库和其中的randint函数(random.randint(1,6))。

  • 从每个骰子中添加数字,并保持每个可能的掷骰计数(2-12),以及你掷出“双打”的次数(两个骰子上的数字相同)。

    < / LI>
  • 打印出每个骰子数量,骰子数量的掷骰数量以及总数的百分比:

    示例输出

    输入卷数:10000

    2 - 266 2.660000%

    3 - 580 5.800000%

    4 - 881 8.810000%

    5 - 1086 10.860000%

    6 - 1418 14.180000%

    7 - 1658 16.580000%

    8 - 1363 13.630000%

    9 - 1096 10.960000%

    10 - 829 8.290000%

    11 - 550 5.500000%

    12 - 273 2.730000%

    双打 - 1425 - 14.234000%

这是我的代码:

import random

def dice_roll():
    return random.randint(1,6)

count = int(input("Enter the number of rolls: "))
number_of_double_dice = 0
results = [0 for x in range(13)]


for i in range(0,count):
    first = dice_roll()
    second = dice_roll()
    sum = first + second

    if(first == second):
        number_of_double_dice = number_of_double_dice + 1
        results[sum] = results[sum] + 1


for x in range (2,13):
    print("{0:d} - {1:d}{2:0.4f}%".format(x, results[x], results[x]/count*100))
    print("Doubles- {0:d}-{{1:0.6f}%".format(number_of_double_dice,       number_of_double_dice*100))

main()

然而,我的代码并没有成功。谁能告诉我为什么?我一直收到ValueError消息。

修改 我运行程序,这是我得到的: 输入卷数:10000

2 - 2772.7700%

双打 - 1667-166700.0000%

3 - 00.0000%

双打 - 1667-166700.0000%

4 - 2502.5000%

双打 - 1667-166700.0000%

5 - 00.0000%

双打 - 1667-166700.0000%

6 - 2982.9800%

双打 - 1667-166700.0000%

7 - 00.0000%

双打 - 1667-166700.0000%

8 - 2732.7300%

双打 - 1667-166700.0000%

9 - 00.0000%

双打 - 1667-166700.0000%

10 - 2782.7800%

双打 - 1667-166700.0000%

11 - 00.0000%

双打 - 1667-166700.0000%

12 - 2912.9100%

双打 - 1667-166700.0000%

这甚至看起来不像我应该拥有的样本输出。我做错了什么?

4 个答案:

答案 0 :(得分:0)

使代码工作需要进行4项更改(除了评论中已经提到的小错别字):

首先,如果1,您只需将results[sum]添加到first == second。因此,results[sum] = results[sum] + 1不应位于if statement

之下
if(first == second):
    number_of_double_dice = number_of_double_dice + 1

results[sum] = results[sum] + 1

其次,由于某些原因我还不知道什么,"{0:d} - {1:d}{2:0.4f}%"应该更改为"{0:d} - {1:d} {2:0.4f}%"(注意第二个}之间的空间增加}和第三个{)。

第三,在计算双打数量的总和百分比时,您忘记除以count。所以,它应该是:

print("Doubles- {0:d}-{1:0.6f}%".format(number_of_double_dice,       number_of_double_dice/count*100))

第四,打印双打数量的行需要放在for statement之外,因为您只想打印一次;而不是for statement的每次迭代:

for x in range (2,13):
    print "{0:d} - {1:d} {2:0.4f}%".format(x, results[x], results[x] / count * 100)

print("Doubles- {0:d}-{1:0.6f}%".format(number_of_double_dice, number_of_double_dice / count *100))

因此,您的代码应该看起来像this: https://gist.github.com/s16h/b4c2bc791880b61418b2

我很快就重写了你的节目;我试图保持你的逻辑和做事方式:

from __future__ import division
import random
import sys

def dice_roll():
    return random.randint(1, 6)

def number_of_rolls():
    return int(raw_input('Enter the number of rolls: '))

def simulate(number_of_times):
    counter = {n : 0 for n in range(2, 13)}
    doubles = 0

    for i in range(number_of_times):
        first_dice = dice_roll()
        second_dice = dice_roll()
        total = first_dice + second_dice

        doubles += 0 if first_dice != second_dice else 1
        counter[total] += 1

    return counter, doubles

def main():
    rolls = number_of_rolls()
    counter, doubles = simulate(rolls)  
    total = sum(counter.values())

    for total, count in counter.items():
        print("{} - {} {:0.4f}%".format(total, count, count / rolls * 100))

    print("Doubles - {} - {:0.6f}%".format(doubles, doubles / rolls * 100))

if __name__ == '__main__':
    sys.exit(main())

我希望这会有所帮助。

答案 1 :(得分:0)

以下是您的代码的更好版本:

import random

num = int(input('How many rolls do you want to simulate? '))

rolls = {}
for k in range(2, 13):
        rolls[k] = 0

doubles = 0

for k in range(num):
        first = random.randint(1, 6)
        second = random.randint(1, 6)
        if first == second:
                doubles+=1
        rolls[first+second]+=1

for k in rolls:
        print('%d - %d %f%%' %(k, rolls[k], float(rolls[k])/float(num)*100))

print('Doubles - %d - %f%%' %(doubles, float(doubles)/float(num)))

运行如下:

How many rolls do you want to simulate? 10000
2 - 286 2.860000%
3 - 571 5.710000%
4 - 788 7.880000%
5 - 1066 10.660000%
6 - 1393 13.930000%
7 - 1731 17.310000%
8 - 1470 14.700000%
9 - 1034 10.340000%
10 - 840 8.400000%
11 - 541 5.410000%
12 - 280 2.800000%
Doubles - 1669 - 0.166900%

你的问题是,如果有双打的话,你只是加了一个,这就是为什么你只有偶数卷的结果。你还有{{1:0.6f}%应该&#39已经{1:0.6f}%

答案 2 :(得分:0)

import random
def roll_two_dice():   
    return random.randint(1, 6)*2           

答案 3 :(得分:0)

import random
def problem2_6(): 
random.seed(82)
    for i in range(100):
        k=random.randint(1,6)
        for j in range(100):
            a=random.randint(1,6)
            s=a+k
            print(s)