vmedhe2
vmedhe2

Reputation: 237

Python calling a function with another function

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

Upvotes: 1

Views: 953

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1124718

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

Upvotes: 4

lucasg
lucasg

Reputation: 11012

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)

Upvotes: 1

Related Questions