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()

First, the usual rejoinder: use the '' 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[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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.