骰子双掷

时间:2018-11-04 20:07:06

标签: python

我的代码当前将2个骰子掷骰1,000次,输出是每个数字被掷骰的次数。但是,我需要改进代码,以便每次滚动double时输出都包括在内。我需要输出类似于的一个示例是:

Snake Eyes were rolled 5 times

Double 2's were rolled 8 times

Double 3's were rolled 17 times 

以此类推,直到6 ...

这是我当前运行良好的代码:

import random

test_data = [0] * 12
n = 1000
for i in range(n):
    result = random.randint(1, 6) + random.randint(1, 6)
    test_data[result - 1] += 1

for i, x in enumerate(test_data, 1):
    print ("Number of ", i, "'s: ", x)

输出为:

Number of  1 's:  0
Number of  2 's:  28
Number of  3 's:  56
Number of  4 's:  82
Number of  5 's:  107
Number of  6 's:  130
Number of  7 's:  172
Number of  8 's:  133
Number of  9 's:  115
Number of  10 's:  93
Number of  11 's:  56
Number of  12 's:  28

如何改善代码,使“双打”也出现?

3 个答案:

答案 0 :(得分:1)

分割线

 result = random.randint(1, 6) + random.randint(1, 6)

进入

roll1 = random.randint(1, 6)
roll2 = random.randint(1, 6)

现在您可以使用以下方法测试双打:

if roll1 == roll2:
   #log double

答案 1 :(得分:0)

您可以为双打创建另一个6个值的列表,并稍稍更改其余代码:

import random

doubles = [0] * 6
test_data = [0] * 12
n = 1000

for i in range(n):
    a,b = random.randint(1, 6) , random.randint(1, 6) # create 2 distinct ones
    result = a+b                                      # and the sum
    test_data[result - 1] += 1
    if a == b:                                        # if a==b count up
        doubles[a-1] += 1


for i, x in enumerate(test_data, 1):
     if i > 1: # no sum of 1 possible
         print ("Number of {:>2}'s: {:>6}".format(i,x))

# output for doubles:
for i,x in enumerate(doubles,1):
    # python ternary operator, see link below
    print( ("Snake eyes were rolled {cnt} times" if i == 1 
            else "Double {}'s were rolled {cnt} times").format(i,cnt=x))

输出:

Number of  2's:     28
Number of  3's:     64
Number of  4's:     72
Number of  5's:    120
Number of  6's:    139
Number of  7's:    139
Number of  8's:    137
Number of  9's:    114
Number of 10's:     93
Number of 11's:     58
Number of 12's:     36
Snake eyes were rolled 28 times
Double 2's were rolled 28 times
Double 3's were rolled 25 times
Double 4's were rolled 23 times
Double 5's were rolled 28 times
Double 6's were rolled 36 times

阅读:

答案 2 :(得分:0)

我将使用Counter来存储这些骰子计数,因为它可以使代码保持整洁:

from collections import defaultdict, Counter
import random

dice_combined = Counter()
dice_individual = defaultdict(Counter)

for i in range(1000):
  a = random.randint(1, 6)
  b = random.randint(1, 6)
  dice_combined[a+b] += 1
  dice_individual[a][b] += 1

# print report:
for i in dice_combined:
  print(i, 'was rolled', dice_combined[i], 'times')

for i in range(1, 6, 1):
  print('double', i, 'was rolled', dice_individual[i][i], 'times')

样本报告

2 was rolled 25 times
3 was rolled 49 times
4 was rolled 77 times
5 was rolled 99 times
6 was rolled 150 times
7 was rolled 169 times
8 was rolled 140 times
9 was rolled 100 times
10 was rolled 101 times
11 was rolled 67 times
12 was rolled 23 times
double 1 was rolled 25 times
double 2 was rolled 29 times
double 3 was rolled 21 times
double 4 was rolled 23 times
double 5 was rolled 28 times