1

Hey im new to python and i am trying to simulate a craps game using a function to call another function. Unfortunately whenever I test my code I always get either 100% wins from all games or 100% losses from all games. I know game, the variable I used to call the craps game, is firing only once and using that same game to count it out. the question is how do I fire the variable game again. here is my code:

def testcraps(n):
    losses=0
    wins=0
    game=quietCraps()
    for x in range(n):    
        game
        print('quietcraps:')
        print(game)
        if game==1:
            wins=wins+1
        if game==0:
            losses=losses+1
        Total=wins/n
        print('wins:')
        print(wins)
        print('Total:')
        print(Total)


def quietCraps():
    import random
    dice=random.randrange(2,12)
    dice2=random.randrange(2,12)
    if dice==7 or dice==11:        
        return 1
    elif dice==2 or dice==3 or dice==12:        
        return 0       
    else:
        while dice2!=dice or dice2!=7:
            dice2=random.randrange(2,12)
            if dice2==dice:
                return 1
            if dice2==7:
                return 0
1
  • You don't appear to be changing the value of game. Try changing the line after your for loop to game = quietCraps() Commented Jun 13, 2013 at 13:27

2 Answers 2

4

You only call quietCraps() once, outside the loop:

game=quietCraps()
for x in range(n):    
    game

The game expression does nothing; no new quietCraps() calls are made. If you want to call a new game for each loop, do:

for x in range(n):    
    game=quietCraps()
Sign up to request clarification or add additional context in comments.

Comments

1

In your code you call quietCraps only once, where you should refresh it :

def testcraps(n):
    losses=0
    wins=0

    for x in range(n):    
        game = quietCraps() # Create a new game !

        print('quietcraps:')
        print(game)
        if game==1:
            wins=wins+1
        if game==0:
            losses=losses+1
        Total=wins/n
        print('wins:')
        print(wins)
        print('Total:')
        print(Total)

On a side note, every import should be on top of the code (it's not an iron rule, more a good practice)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.