掷两个骰子并将结果制成表格

2024-05-02 06:04:50 发布

您现在位置:Python中文网/ 问答频道 /正文

问题是:

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

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

  • 把每一个骰子上的数字相加,记录下每一个可能的掷骰次数(2-12次),以及你掷“双倍”的次数(两个骰子上的数字相同)。

  • 打印出每个骰子数、骰子数的卷数以及占总数的百分比:

    样本输出

    输入卷数:10000

    2-2662.660000%

    3-580 5.800000%

    4-881810000%

    5-1086 10.86万%

    6-1418 14.180000%

    7-1658 1658万

    8-1363 13.63万%

    9-1096 10.960000%

    10-829 8.290000%

    11-5505.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()

但是,我的代码不工作。有人能告诉我为什么吗?我不断收到价值错误信息。

编辑 我运行这个程序,得到的是: 输入卷数: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%

这甚至不像示例输出所说的那样。我做错什么了?


Tags: ofinnumberforcountrangerandom骰子
3条回答

要使代码正常工作,需要进行4项更改(注释中已经提到的小错误除外):

首先,仅当first == second时,才将1添加到results[sum]。所以,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())

我希望这能有帮助。

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

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}%这应该是{1:0.6f}%

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

相关问题 更多 >