1

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?

2
  • The double opening bracket {{1:0.6f}% should be a single. May 11, 2014 at 23:29
  • 1
    Where is the ValueError raised? Also, the call to main() would raise a NameError unless you have actually defined it.
    – s16h
    May 11, 2014 at 23:40

4 Answers 4

1

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.

6
  • Even with that fix, still gives me the same format of results, although with different numbers due to the random library.
    – Kjc21793
    May 12, 2014 at 0:00
  • Printing the number of doubles should not be in the for statement; you only want to print it once, at the end; not in every iteration of the for statement.
    – s16h
    May 12, 2014 at 0:08
  • 1
    Generally, a list isn't the best (i.e. most readable) way of representing this problem; I think a dictionary is more well-suited.
    – s16h
    May 12, 2014 at 0:09
  • That program works. What was the difference between yours and mine? I mean, what wasn't I doing correctly that you were?
    – Kjc21793
    May 12, 2014 at 0:15
  • The math seems off. For example, if I enter one roll as an input, it will give me data for 2 rolls.
    – Kjc21793
    May 12, 2014 at 19:19
0

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}%.

3
  • Better to use str.format() rather than the printf-style String Formatting.
    – s16h
    May 12, 2014 at 0:16
  • "You also multiplied by 100 instead of dividing by 100"? You are multiplying by 100 too; which is correct.
    – s16h
    May 12, 2014 at 0:20
  • Can you post the full traceback?
    – A.J. Uppal
    May 12, 2014 at 0:24
0
import random
def roll_two_dice():   
    return random.randint(1, 6)*2           
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)
3
  • 5
    Welcome to Stack Overflow! Code-only answers are not particularly helpful. Please add some descriptions of how this code solves the problem.
    – 4b0
    May 30, 2021 at 12:11
  • 3
    And please make sure the code you do share is correct. This code will generate an IndentationError.
    – Chris
    May 30, 2021 at 14:09
  • To expand on @Shree's point, take a look at the accepted answer as a good example of the level of explanation to provide. May 31, 2021 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.