Please use a python program when solving. maybe take a screen shot of your finis
ID: 3716713 • Letter: P
Question
Please use a python program when solving. maybe take a screen shot of your finished program so i can see your work. otherwise its hard to understand the syntax with just copy and paste
Thank You
7. Craps is a dice game played at many casinos. A player rolls a pair of normal six-sided dice. If the initial roll is 2, 3, or 12, the player loses. If the roll is 7 or 11, the player wins. Any other initial roll causes the player to "roll for point." That is, the player keeps rolling the dice until either rolling a 7 or re-rolling the value of the initial roll. If the player re-rolls the initial value before rolling a 7, it's a win. Rolling a 7 first is a loss. Write a program to simulate multiple games of craps and estimate the probability that the player wins. For example, if the player wins 249 out of 500 games, then the estimated probability of winning is 249/500 0.498Explanation / Answer
Please find my implementation.
#The python program that calls testCraps method and prints
#the fraction of games the player won the game.
import random
def main():
# set n value
n=10000;
#call testCraps with n value
print testCraps(n)
#The method roll that gerates the sum of rolling to dices and returns total
def roll():
total = (random.randrange(1,7) + random.randrange (1,7))
return total
#The method testCraps that calls the roll method and
#count the number of times the user won the game for given
#number of times of n value.
def testCraps(n):
dice = roll()
totalCount = 0
won = 0
#roll for n values
for i in range (n):
#add number of times totalCount
totalCount = totalCount + 1
#check dice vlaue is either 2 , 3 or 12 then player lose
if dice == 2 or dice == 3 or dice == 12:
won = won + 0
#check if player roll is 7 or 11
elif dice == 7 or dice == 11:
#increment won by 1
won += 1
else:
#otherwise roll until user roll is 7 or intial dice roll
dice1 = roll()
while True:
#print dice1
if dice1 == 7:
break
elif dice1 == dice:
won += 1
break
else:
dice1 = roll()
#print dice1
#return the fraction of won to totalCount
return((float(won)/totalCount))
#calling main method
main()