| | |
Python Craps Game
![]() |
•
•
Join Date: Apr 2008
Posts: 2
Reputation:
Solved Threads: 0
I'm new to Python and I'm working on a craps game. I got the basic code down, but I need to simulate a total of 200 games and track the number of wins. I need a counter in my program, but I can't figure out how to do this. Here's what I have so far:
import random
def roll():
die1 = random.randrange(1,7)
die2 = random.randrange(1,7)
print die1, ", ", die2
total = die1 + die2
return total
def main():
firstTry = roll()
print firstTry
if firstTry == 7 or firstTry == 11:
print "You win!"
elif firstTry == 2 or firstTry == 3 or firstTry == 12:
print "You lose."
else:
i = roll()
print i
while i != firstTry:
if i == 7:
print "You lose."
break
else:
i = roll()
if i == firstTry:
print "You win!"
main()
import random
def roll():
die1 = random.randrange(1,7)
die2 = random.randrange(1,7)
print die1, ", ", die2
total = die1 + die2
return total
def main():
firstTry = roll()
print firstTry
if firstTry == 7 or firstTry == 11:
print "You win!"
elif firstTry == 2 or firstTry == 3 or firstTry == 12:
print "You lose."
else:
i = roll()
print i
while i != firstTry:
if i == 7:
print "You lose."
break
else:
i = roll()
if i == firstTry:
print "You win!"
main()
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
First, the usual rejoinder: use the '[code=Python ]' and '[/code ]' tags to make your posts look pretty.
Now for the code: why not turn main() into a function called game() that returns True for a win and False for a loss. Then you can run a for loop and add up the True's (which have a value of 1).
Jeff
Now for the code: why not turn main() into a function called game() that returns True for a win and False for a loss. Then you can run a for loop and add up the True's (which have a value of 1).
Jeff
•
•
Join Date: Apr 2008
Posts: 2
Reputation:
Solved Threads: 0
Hi Jeff,
Thanks for your suggestion, but I figured it out a different way. In my play(n) function, I intialized values for my game counter (k=0) and my wins counter (wins=0). Then for each instance in the program where I would win or lose, I increment k by 1, and each instance where I win a game, I increment wins by 1. In my main() program, I call the play(n) function, and print the return of play(n). Here's my code:
from random import randrange
def play(n):
k = 0
wins = 0
while k <= n:
firsttry = roll()
if firsttry == 7 or firsttry == 11:
wins = wins +1
k = k+1
elif firsttry == 2 or firsttry == 3 or firsttry == 12:
k = k+1
else:
i = roll()
while i != firsttry:
if i == 7:
k = k+1
break
else:
i = roll()
if i == firsttry:
wins = wins+1
k = k+1
while k == n:
break
return wins
def roll():
die1 = randrange(1,7)
die2 = randrange(1,7)
total = die1 + die2
return total
def main():
print "This program simulates a game of craps. By entering "
print "the number of games you wish to play, it will calculate "
print "The number of games won."
print
try:
numGames = input("Enter the number of games to play: ")
if numGames <=0:
print "\nYou must enter a value greater than zero. Please try again."
else:
print "The number of games won is",play(numGames),"out of",numGames
except NameError:
print "\nYou must enter a numeric value. Please try again."
except SyntaxError:
print "\nYou made a syntax error. Please try again."
if __name__ == '__main__':
main()
You'll notice I also added some exception handling in my code for unusual input.
Thanks so much for taking the time to respond.
Carrie
Thanks for your suggestion, but I figured it out a different way. In my play(n) function, I intialized values for my game counter (k=0) and my wins counter (wins=0). Then for each instance in the program where I would win or lose, I increment k by 1, and each instance where I win a game, I increment wins by 1. In my main() program, I call the play(n) function, and print the return of play(n). Here's my code:
from random import randrange
def play(n):
k = 0
wins = 0
while k <= n:
firsttry = roll()
if firsttry == 7 or firsttry == 11:
wins = wins +1
k = k+1
elif firsttry == 2 or firsttry == 3 or firsttry == 12:
k = k+1
else:
i = roll()
while i != firsttry:
if i == 7:
k = k+1
break
else:
i = roll()
if i == firsttry:
wins = wins+1
k = k+1
while k == n:
break
return wins
def roll():
die1 = randrange(1,7)
die2 = randrange(1,7)
total = die1 + die2
return total
def main():
print "This program simulates a game of craps. By entering "
print "the number of games you wish to play, it will calculate "
print "The number of games won."
try:
numGames = input("Enter the number of games to play: ")
if numGames <=0:
print "\nYou must enter a value greater than zero. Please try again."
else:
print "The number of games won is",play(numGames),"out of",numGames
except NameError:
print "\nYou must enter a numeric value. Please try again."
except SyntaxError:
print "\nYou made a syntax error. Please try again."
if __name__ == '__main__':
main()
You'll notice I also added some exception handling in my code for unusual input.
Thanks so much for taking the time to respond.
Carrie
![]() |
Other Threads in the Python Forum
- Previous Thread: Help with Global Variables - i think
- Next Thread: Importing problems
| Thread Tools | Search this Thread |
accessdenied advanced apache application argv array beginner book change command converter countpasswordentry csv curved dan08 def dictionary dynamic edit enter event examples file float format function google gui homework import inches input jaunty java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric obexftp output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pygtk pyopengl python random recursion redirect remote return reverse scrolledtext session simple skinning software sprite statictext string strings syntax terminal text threading time tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable voip wordgame wxpython





