November 8th, 2013, 09:44 AM

Craps Program
I wrote this program to find the probability of winning a certain amount of craps games.
The output is completely off, I usually get output ranging from 6.09.0, it should be around 0.5.
If anybody could find the error I will be very thankful. I looked for a couple hours
and changed parts of it to see what it could be, but I got nothing.
Code:
from random import randrange
def rollTwoDice():
rollTwoDice = randrange(1,7) + randrange(1,7)
return rollTwoDice
def playOneGame():
n = rollTwoDice()
if n == 7 or n == 11:
return True
elif n == 2 or n == 3 or n == 12:
return False
else:
m = rollTwoDice()
while n != 7 or n != m :
if n == 7:
return False
elif n == m:
return True
else:
m = rollTwoDice()
def playManyGames(howMany):
win = lose = 0
for i in range(howMany):
if playOneGame() == True:
win += 1
else:
lose += 1
return win, lose
def main():
howMany = eval(input("How many games would you like to play? "))
win, lose = playManyGames(howMany)
print("The probability of winning is ", win,"/",lose," = ",(win/lose), sep='')
if __name__=="__main__":
main()
November 8th, 2013, 10:14 AM

Well chosen font. Thanks.
Compute probability using the total number of games. Since you're using python3 you don't need to worry about integer divisions.
Code:
probability_to_win = win/(win+lose)
# or
probability_to_win = win/howMany
[code]
Code tags[/code] are essential for python code and Makefiles!
November 8th, 2013, 10:38 AM

The program contains wrong logic.
simplified, and corrected based on my craps understanding:
Code:
from random import randrange
def rollTwoDice():
rollTwoDice = randrange(1,7) + randrange(1,7)
return rollTwoDice
def playOneGame(): # return True iff loss, else False.
point = rollTwoDice()
if point in (7, 11):
return True
if point in (2, 3, 12):
return False
m = rollTwoDice()
while m not in (7, point):
m = rollTwoDice()
return m == point
def playManyGames(howMany):
return sum(playOneGame() for i in range(howMany))
def main():
howMany = eval(input("How many games would you like to play? "))
win = playManyGames(howMany)
print("The probability of winning is ", win,"/",howMany," = ",(win/howMany), sep='')
if __name__=="__main__":
main()
[code]
Code tags[/code] are essential for python code and Makefiles!
November 8th, 2013, 11:48 AM
