Rolling two dice and tabulating outcomes
问题是:
你将编写一个程序来模拟一对骰子的滚动。
您将询问用户要模拟的辊数。然后你将每卷掷两个骰子。对每个骰子使用随机库和其中的randint函数(random.randint(1,6))。
将每个骰子的数字相加,并对每个可能的掷骰子(2-12)进行计数,以及掷"双骰子"的次数(两个骰子上的数字相同)。
打印出每个骰子计数、该骰子计数的卷数和总数的百分比:
样本输出
输入辊数:10000
2-266 2.66万%
3-580 5.80万%
4-881 8.81万%
5-1086 10.86万%
6-1418 14.18万%
7-1658 16.58万%
8-1363 13.63万%
9-1096 10.96万%
10-829 8.290000%
11-550 5.500000%
12-273 2.73万%
双打-1425-14.234000%
以下是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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—0%
双打-1667-166700.0000%
4—2502.5000%
双打-1667-166700.0000%
5—0%
双打-1667-166700.0000%
6—2982.9800%
双打-1667-166700.0000%
7—0%
双打-1667-166700.0000%
8—2732.7300%
双打-1667-166700.0000%
9—0%
双打-1667-166700.0000%
10—2782.7800%
双打-1667-166700.0000%
11—0%
双打-1667-166700.0000%
12—2912.9100%
双打-1667-166700.0000%
这甚至不像样本输出所说的那样。我做错什么了?
1 2 3 | import random def roll_two_dice(): return random.randint(1, 6)*2 |
下面是您的代码的更好版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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))) |
运行如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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% |
你的问题是,如果有双打,你只会在总数上加一个,这就是为什么你只有双打的结果。你也有
要使代码正常工作,需要进行4次更改(注释中已提到的小错误除外):
首先,如果
1 2 3 4 | if(first == second): number_of_double_dice = number_of_double_dice + 1 results[sum] = results[sum] + 1 |
其次,由于某些原因,我还不知道是什么原因,应该将
第三,在计算双打总数的百分比时,忘记了用
1 | print("Doubles- {0:d}-{1:0.6f}%".format(number_of_double_dice, number_of_double_dice/count*100)) |
第四,打印双精度数的行需要放在
1 2 3 4 | 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)) |
所以您的代码应该是这样的:https://gist.github.com/s16h/b4c2bc791880b61418b2。
我很快就重写了你的程序;我试图保持你的逻辑和做事方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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()) |
我希望这有帮助。