Question

The problem is to simulate a number of games of craps. You roll two 6-sided dice. If the roll adds up to 2, 3, or 12, the player loses that game. If the roll adds up to 7 or 11, the player wins. If the roll adds up to any other number, the player re-rolls until either the previous roll amount is rolled again, or a 7 is rolled. If a 7 is rolled first, the game counts as a win. If the previous roll amount is rolled first, the game counts as a lose. We are supposed to show the amount of wins and percentage of the total.

I'm not too sure what I have wrong here - the percentage of winning should be around 50%, but when I run the program, I normally get between 1-3% winning. Edit: I'm actually not sure if the probability is supposed to be 50%, but I'm pretty sure it's not supposed to be 1-3%...

from random import *

def craps():
printIntro()
n=getInput()
winCount=simRoll(n)
printResults(n, winCount)


def printIntro():

print('This program simulates the popular casino game "craps." A player rolls a pair of normal six-sided dice.')
print('If the initial roll is a 2, 3, or 12, the player loses. If the initial roll is a 7 or 11, the player wins.')
print('Any other initial roll causes the player to "roll for point." The player keeps rolling the dice until either rolling')
print('a 7 or re-rerolling the value of the initial roll. If the player re-rolls the initial value before rolling a 7, it is a win.')
print('If the player rolls a 7 first, it is a loss.')
print('\nThis program simulates n games of craps and calculates the percent of games won.')


def getInput():

n=eval(input("Input the amount of games of craps to simulate: "))
return n


def simRoll(n):
rollCount=0
winCount=0
PointForRoll=0

while rollCount < n:

        rollCount=rollCount+1

        randomRoll=randrange(1,7) + randrange (1,7)

        if randomRoll == 2 or randomRoll == 3 or randomRoll == 12:
            winCount = winCount + 0

        if randomRoll == 7 or randomRoll == 11:
            winCount = winCount + 1

        else:

            while PointForRoll != 7 or PointForRoll != randomRoll:

                PointForRoll = randrange(1,7) + randrange(1,7)

                if PointForRoll == randomRoll:
                    winCount=winCount

                if PointForRoll == 7:
                    winCount=winCount+1

                return PointForRoll

return winCount


def printResults(n, winCount):

print('\nFor', n, 'games of craps simulated,', winCount, 'games were wins, giving a success rate of '+ str(100*(winCount/n)) + '%.')


if __name__ == '__main__': craps()

Thank you!

Was it helpful?

Solution

if randomRoll == 2 or randomRoll == 3 or randomRoll == 12:
    winCount = winCount + 0
if randomRoll == 7 or randomRoll == 11:
    winCount = winCount + 1
else:

The second if should be elif. You don't want the else to be entered if the roll is 2, 3, or 12.

while PointForRoll != 7 or PointForRoll != randomRoll:

The or should be and. While the roll isn't 7 and the roll is not the point.

if PointForRoll == randomRoll:
    winCount=winCount
if PointForRoll == 7:
    winCount=winCount+1

This is backwards. Rolling a 7 is a loss. Hitting the point is a win. You should be incrementing the win count in the first if.

return PointForRoll

Delete this line. You should not be returning from this loop.

while PointForRoll != 7 and PointForRoll != randomRoll:
    ...

Finally, you never reset PointForRoll after this loop ends. Add PointForRoll = 0 either right before or right after the loop.

With all of these changes made, I get a win rate right around 50%.

OTHER TIPS

Right now, if you roll a 2, 3 or 11, your program still tries to make point. Instead of:

if randomRoll == 2 or randomRoll == 3 or randomRoll == 12:
   ....

if randomRoll == 7 or randomRoll == 11:
  ...

else:
  ...

You want to try

if randomRoll == 2 or randomRoll == 3 or randomRoll == 12:
   ....

elif randomRoll == 7 or randomRoll == 11:
  ...

else:
 ....

This way rolls of 2, 3 and 12 won't use the code in your final 'else'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top