Python Craps Game

Reply

Join Date: Apr 2008
Posts: 2
Reputation: CFrances is an unknown quantity at this point 
Solved Threads: 0
CFrances CFrances is offline Offline
Newbie Poster

Python Craps Game

 
0
  #1
Apr 26th, 2008
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()
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Python Craps Game

 
0
  #2
Apr 27th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 2
Reputation: CFrances is an unknown quantity at this point 
Solved Threads: 0
CFrances CFrances is offline Offline
Newbie Poster

Re: Python Craps Game

 
0
  #3
Apr 27th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC