Question

Here's the problem:

You will write a program to simulate the rolling of a pair of dice.

  • You will ask the user for the number of rolls to simulate. You will then roll two dice per roll. Use the random library and the randint function therein (random.randint(1,6)) for each dice.

  • Add the numbers from each dice, and keep a count of each possible roll (2-12), and how many times you roll “doubles” (the same number on both dice).

  • Print out each dice count, the number of rolls for that dice count, and the percentage of the total:

    Sample Output

    Enter the number of rolls: 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%

    Doubles - 1425 - 14.234000%

Here's my code:

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()

However, my code isn't working out. Can anyone tell me why? I keep getting ValueError messages.

EDIT I Run the program, and here is what I get: Enter the number of rolls: 10000

2 - 2772.7700%

Doubles- 1667-166700.0000%

3 - 00.0000%

Doubles- 1667-166700.0000%

4 - 2502.5000%

Doubles- 1667-166700.0000%

5 - 00.0000%

Doubles- 1667-166700.0000%

6 - 2982.9800%

Doubles- 1667-166700.0000%

7 - 00.0000%

Doubles- 1667-166700.0000%

8 - 2732.7300%

Doubles- 1667-166700.0000%

9 - 00.0000%

Doubles- 1667-166700.0000%

10 - 2782.7800%

Doubles- 1667-166700.0000%

11 - 00.0000%

Doubles- 1667-166700.0000%

12 - 2912.9100%

Doubles- 1667-166700.0000%

This is not even close to looking like what the sample output says I should have. What am I doing wrong?

Was it helpful?

Solution

There are 4 changes required to make your code work (other than the minor typos already mentioned in the comments):

Firstly, you only add 1 to results[sum] if first == second. So, results[sum] = results[sum] + 1 should not be under the if statement:

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

results[sum] = results[sum] + 1

Secondly, for some reason that I don't yet know what, "{0:d} - {1:d}{2:0.4f}%" should be changed to "{0:d} - {1:d} {2:0.4f}%" (notice the added space between the second } and the third {).

Thirdly, when calculating the percentage of total for the number of doubles, you have forgotten to divide by count. So, it should be:

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

Fourthly, the line where you print the number of doubles needs to be put outside the for statement because you only want to print it once; not on every iteration of the 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))

So your code should look like this: https://gist.github.com/s16h/b4c2bc791880b61418b2.

I have quickly re-written your program; I have tried to keep your logic and way of doing things:

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())

I hope this helps.

OTHER TIPS

Here is a better version of your code:

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)))

This runs as:

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%

Your problem was that you were only adding one to the sum if there were doubles, which was why you only had results for even rolls. You also had {{1:0.6f}% which should've been {1:0.6f}%.

import random
def roll_two_dice():   
    return random.randint(1, 6)*2           
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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top