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.