Search code examples
pythonpercentagecalculation

Cumulative Percentage stuck at 0.00% at the first roll


I wrote a code which gives the stats of 1,000,000 games of craps. I calculated the percentage of the total games that were won and lost, and also the the cumulative percentage of the total games played that were won or lost up to and including a given number of rolls (column 3 of the output)

The problem is that it keep giving me 0.00% on the first roll, why is that happening?

The code where I calculate the cumulative percentage:

for i in range(0, len(rolls)):
    p_resolved = rolls[i]*100/craps_game_number
    c_resolved = 100*sum(rolls[0:i])/craps_game_number

    if i!=len(rolls)-1:
        print('{:>5}{:>19.2f}%{:>19.2f}%'.format(i+1,p_resolved,c_resolved))
    else:
        print('{:>5}{:>19.2f}%{:>19.2f}%'.format(i + 1, p_resolved, 100*sum(rolls)/craps_game_number))

enter image description here

Here is the whole code if needed:

import random

def win_or_lose():
    roll = random.randint(1,6)+random.randint(1,6)
    if roll in [7,11]:
        return 'Win',1 # return the outcome and attempts it took to decide
    elif roll in [2,3,12]:
        return 'Loss',1
    else:
        attempts=1
        my_point=roll
        while True:
            roll=random.randint(1,6)+random.randint(1,6)
            attempts+=1 # add 1 until the outcome is decided
            if roll==my_point:
                return 'Win',attempts # return the outcome and rounds
            elif roll==7:
                return 'Loss',attempts # return the outcome and rounds
            else:
                continue

rolls = [0]*25
wins=loss=0
craps_game_number=1000000
for _ in range(craps_game_number):
    outcome,attempts = win_or_lose() # store the outcome and rounds it took to decide
    if outcome=='Win':
        wins+=1
    else:
        loss+=1
    if attempts<=len(rolls):
        rolls[attempts-1]+=1
    else:
        rolls[-1]+=1

print('Percentage of wins: {:.2f}%'.format(wins*100/(craps_game_number)))
print('Percentage of losses: {:.2f}%'.format(loss*100/(craps_game_number)))
print('Percentage of wins/losses based on total number of rolls')
print()
print('{:>25}{:>20} '.format('% Resolved','Cummulative %'))
print('{:<5}{:>20} {:>20}'.format('Rolls','on this roll','of games resolved'))

for i in range(0, len(rolls)):
    p_resolved = rolls[i]*100/craps_game_number
    c_resolved = 100*sum(rolls[0:i])/craps_game_number

    if i!=len(rolls)-1:
        print('{:>5}{:>19.2f}%{:>19.2f}%'.format(i+1,p_resolved,c_resolved))
    else:
        print('{:>5}{:>19.2f}%{:>19.2f}%'.format(i + 1, p_resolved, 100*sum(rolls)/craps_game_number))

Solution

  • If you change this line:

    c_resolved = 100*sum(rolls[0:i])/craps_game_number
    

    ... to this:

    c_resolved = 100*sum(rolls[0:i + 1])/craps_game_number
    

    ... you should get output that is something like this, which I think is what the question is asking for:

                   % Resolved       Cummulative %
    Rolls        on this roll    of games resolved
        1              33.35%              33.35%
        2              18.80%              52.15%
        3              13.49%              65.64%
        4               9.64%              75.28%
        5               6.91%              82.19%
        6               4.98%              87.16%
        7               3.57%              90.73%
        8               2.58%              93.31%
        9               1.85%              95.16%
       10               1.35%              96.51%
       11               0.97%              97.48%
       12               0.69%              98.17%
       13               0.50%              98.67%
       14               0.36%              99.03%
       15               0.26%              99.29%
       16               0.19%              99.48%
       17               0.14%              99.62%
       18               0.10%              99.72%
       19               0.08%              99.80%
       20               0.05%              99.85%
       21               0.04%              99.89%
       22               0.03%              99.92%
       23               0.02%              99.94%
       24               0.02%              99.96%
       25               0.04%             100.00%
    

    This will make the first line of cumulative result non-zero, and should also correct the values of all subsequent lines prior to the last one (which was getting set to 100% via special-case logic).