1

I am a beginner programmer and I need help with some parts of my code. I am currently creating a craps game simulator, but it appears that my code wont really run. My code is attached and for a few notes, every time there is a roll of the dice, the user must hit enter which will cause the program to roll the dice.

For a brief overview, here is some of the rules behind craps:

Each round has two phases: "come-out" and "point". To start a round, the shooter makes one or more "come-out" rolls. A come-out roll of 2, 3 or 12 loses and is called "crap-out". A come-out roll of 7 or 11 (a "natural") wins. The other possible numbers are the point numbers: 4, 5, 6, 8, 9, and 10. If the shooter rolls one of these numbers on the come-out roll, this establishes the "point" and continues to the point phase. At the point-phase, if the user rolls the same number as the come-out phase, they "Hit" or win the game. If they roll a 7 however, they "seven-out" or lose the game. If the player doesn't get a 7 or the same come-out number, they keep rolling until they either hit or seven-out.

My issue is that when the program runs, i am able to get Please hit enter, but when I hit enter, it wont continue to the next part of the code which will roll the dice. I can't quite figure out why that happens. Also, I may need some help looking at the logic behind my code, I am not entirely sure if when implemented, the desired results would occur. Any help is appreciated!

import random

def playRound():

    #This will print out the current phase.

    print "The come-out phase:"
    print 

    #This will tell the user to hit enter to roll the dice.

    rollDice = raw_input("Hit ENTER to roll the dice...")

    #this will ensure that when a user hits enter, the program moves on.

    if rollDice == rollDice:

        #this will sum up two random integers to simulate two dice being thrown and record         the total.

        diceTotal = random.randint(1,6) + random.randint(1,6)

        #this will see if the numbers are 7 or 11, and if so, will let the user know they won.

        if diceTotal in (7,11):

            return "You rolled a", diceTotal
            return "You Win: Natural!"

        #this will see if numbers are 2, 3, or 12, and if so, will let user know they lost.

        if diceTotal in (2,3,12):

            return "You rolled a", diceTotal
            return "You Lose: Crap-Out!"

        #let user know what they rolled if conditions above are not met.

        return "You Rolled a", diceTotal

        #This will now start the point phase.

        print "The Point Phase:"
        print

        #this will ask the user to hit enter to roll the dice.

        rollDice = raw_input("Hit ENTER to roll the dice...")

        #this will ensure that when the user hits enter, the dice will roll.

        if rollDice == rollDice:

            #this will add up the sum of two random numbers simulating dice and save to variable.

            diceTotalPoint = random.randint(1,6) + random.randint(1,6)

            #this will see if the roll is not 7 or the diceTotal from come-out phase.

            while diceTotalPoint not in (7, diceTotal):

                #This will continue to roll the dice, if 7 or the come-out phase number is not achieved.

                rollDice = raw_input("Hit ENTER to roll the dice...")

                if rollDice == rollDice:

                    diceTotalPoint = random.randint(1,6) + random.randint(1,6)

            #this will tell the user that if the dice roll is the same as the come-out phase,           it will be a hit and they win.

            if diceTotalPoint == diceTotal:

                return "You Rolled a", diceTotalPoint
                return "You Win: Hit!"

            #this will tell the user if they get a 7, and tell them they lose.

            if diceTotalPoint == 7:

                return "You Rolled a", diceTotalPoint
                return "You lose: Seven-Out!"
Jr.
  • 111
  • 5
  • 12
  • 1
    "My issue is, why doesn't my program completely run, and where are the flaws." You are supposed to tell us why your program doesn't run and where the flaws are. That's not our job. – Shashank Oct 10 '13 at 03:13
  • sorry for that, I am still new here and I am learning how to be better at asking these questions. Won't happen again, and I realize that this is to get help and learn. – Jr. Oct 10 '13 at 03:18

2 Answers2

0

Some error messages might help the readers.

I think your problem is that you should print everywhere that you are currently using return. See Why would you use the return statement in Python?.

Other notes: if rollDice == rollDice: will always be true - why even include it?

Community
  • 1
  • 1
Frederick
  • 1,271
  • 1
  • 10
  • 29
0

For starters, a return statement bails out of the function it is in. So

        return "You rolled a", diceTotal
        return "You Lose: Crap-Out!"

Never will get to "You Lose: Crap-Out!" since it skips out on the first return value. Use print instead.

I echo Frederick's comment about the if rollDice == rollDice part, no need to put that into an if statement at all since it will always run.

Overall this is a beast of a function. I would suggest splitting this up into multiple functions, or better yet classes to make things easier to manage. Right now it's a god function (http://iwanttocode.wordpress.com/tag/god-function/), which is just begging for pain wrt maintenance or debugging. Also think about how with the code you posted there is 0 reusable code if you want to write another program for another dice game.

BWStearns
  • 2,567
  • 2
  • 19
  • 33