3

I'm trying to implement a function craps() that takes no argument, simulates one game of craps, and returns 1 if the player won and 0 if the player lost.

Rules of the game: the game starts with the player throwing a pair of dice. If the player rolls a total of 7 or 11, the player wins. If the player rolls a total of 2,3 or 12, the player loses. For all other roll values the games goes on until the player either rolls the initial value agaian (in which case the player wins) or 7 (in which the player loses).

I think I'm getting closer but I'm not there yet, I don't think I got the while loop working correctly yet. This is the code I got so far:

def craps():
    dice = random.randrange(1,7) + random.randrange(1,7)
    if dice in (7,11):
        return 1
    if dice in (2,3,12):
        return 0
    newRoll = craps()
    while newRoll not in (7,dice):
        if newRoll == dice:
            return 1
        if newRoll == 7:
            return  0

How do I fix the while loop? I really can't find the issue with it but I know it's wrong or incomplete.

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
Snarre
  • 597
  • 5
  • 14
  • 22

3 Answers3

5

You never get to the while loop because of this line:

newRoll = craps()   

That's as far as it gets. So it will only do the top part of the craps() function. You need to just use the same roll code from earlier. I think you want something like:

newRoll = random.randrange(1,7) + random.randrange(1,7)
while newRoll not in (7,dice):
    newRoll = random.randrange(1,7) + random.randrange(1,7)        

if newRoll == dice:
    return 1
if newRoll == 7:
    return  0
ecline6
  • 896
  • 8
  • 15
  • thanks, one question though. Do I need the second copy of this code?: newRoll = random.randrange(1,7) + random.randrange(1,7) (the one in the while loop) – Snarre May 22 '13 at 19:46
3

Rules of the game: the game starts with the player throwing a pair of dice. If the player rolls a total of 7 or 11, the player wins. If the player rolls a total of 2,3 or 12, the player loses. For all other roll values the games goes on until the player either rolls the initial value agaian (in which case the player wins) or 7 (in which the player loses).

def rollDice(): # define a function to generate new roll
    return random.randrange(1,7) + random.randrange(1,7)

def craps():
    firstRoll= rollDice()
    if firstRoll in (7,11): 
        return 1 # initial winning condition
    if firstRoll in (2,3,12):
        return 0 #initial losing condition

    while True:
        newRoll = rollDice()
        if newRoll == firstRoll: 
            return 1 #secondary winning condition
        if newRoll == 7: 
            return 0 #secondary losing condition

then you can call craps() whenever you want to play some craps and its output will be 1 or 0 if it won or loss.

TehTris
  • 3,139
  • 1
  • 21
  • 33
  • This implementation looks better than the rest. – Noctis Skytower May 22 '13 at 21:07
  • This is **exactly** what I was going to do; there's no duplicate code and the common tasks are split into easily maintainable functions. Got my upvote, and totally deserves to be accepted. –  May 22 '13 at 23:14
1

You're recursively calling craps, but that won't work since the function returns 1 or 0. You need to add the actual dice rolling to your while loop.

newRoll = random.randrange(1,7) + random.randrange(1,7)
while newRoll not in (7,dice):
    newRoll = random.randrange(1,7) + random.randrange(1,7)

if newRoll == dice:
    return 1
else:
    return  0
voithos
  • 68,482
  • 12
  • 101
  • 116
  • thanks, one question though. Do I need the second copy of this code?: newRoll = random.randrange(1,7) + random.randrange(1,7) (the one in the while loop) – Snarre May 22 '13 at 19:45
  • @Snarre: Yes, because you want to *keep rolling* until the `while` condition holds. – voithos May 22 '13 at 19:47
  • ok, so the first one, before the while loop, only helps us define the condition? – Snarre May 22 '13 at 19:54
  • @Snarre: The first one is the "initial test", the other one is for when the initial test fails. You do actually need both of those, as well as the first one at the start of `craps`, because the *very* first one has a different winning condition (namely, 7 or 11). – voithos May 22 '13 at 19:55
  • @Snarre: In many other languages, there exists a "do-while" loop, which performs an operation and only **then** checks a condition. The creators of Python decided that they didn't want such a thing in their language, so that's why the dice roll has to be duplicated around the `while` loop. – voithos May 22 '13 at 19:56
  • shouldnt there be a `if newRoll == 11: return 1` before the `while` loop to check for if it rolled 11? or losing? – TehTris May 22 '13 at 20:21
  • @TehTris: If you mean at the beginning of the function, yes. The section that I've shown is simply the replace for his `while` loop. – voithos May 22 '13 at 20:23