I am writing a Crap Game. The first part turn out fine but I have problem with this requirement "Your program must keep track of and report the number of games won by the user and the number of games won by the computer ("house"). The score should be 0-0 initially and must be updated after each game. The user simply enters the word "roll" every time (s)he wants to proceed. Entering "done" ends the game"

When I enter roll to play the game again,my program will not start from very beginning, it just count it as a sencond roll. I think it is the problem with where I should put the "raw_input"part. Can anyone help me with this?

Thanks a lot !

This is what I have so far.

import random

def rollDice():
    d1 = random.randrange( 1, 7 )
    d2 = random.randrange( 1, 7 )
    total = d1 + d2
    print "D1 is ",d1, "and D2 is ",d2," total is ",total

    return total
score_yours = 0
score_house = 0
sum = rollDice() # first dice roll

if sum == 7 or sum== 11: # win on first roll
    print "You win on first roll."
elif sum == 2 or sum == 3 or sum == 12: #lose on first rool
    score_house = score_house+1
    print "You lose on first roll."
    game = "CONTINUE" # remember point
    Point= sum
    print "First roll, point is", Point

    while game == "CONTINUE": # keeep rolling
        sum = rollDice()

        if sum == Point: #win by making point
            print "Match the point, you win."

        elif sum == 7:
            score_house = score_house+1
            print "Got a 7, you lose."

        a = raw_input ("Do you want to continue?")
        if a == 'roll':
            sum = rollDice()
        if a == "done":
            print "You won ",score_yours,"times; and house won ",score_house,"times"

What you've done is to make the game itself into your main code. If you think about it, that's not what you want. The main loop here is

playerscore = housescore = 0
while user wants to play:
    winner = CrapsGame()  # note the 's' -- a Crap Game is something else entirely :).
    if winner == "player":
        playerscore += 1
    elif winner == "house":
        housescore += 1

print playerscore and housescore

In other words, the game itself should be a function that returns the winner.


This article has been dead for over six months. Start a new discussion instead.