paxyshack
paxyshack

Reputation: 57

Using a variable from one function in another function (parameter 'dice' unfilled)

Hi im trying to create a game where the computer generates 5 random numbers between 1 and 6. but my issue is i have created a list which will gain '1' in its respective section depending on what number comes up. e.g. if the computer generates 31534 the list needs to show [1,0,2,1,1,0] (because there was two 3's it fills 2 in the 3 slot) it only displays the 5 random numbers and nothing else

from random import randint

def rollDice():

    dice = [str(randint(1, 6)) for _ in range(5)]
    print(dice)
    return dice

#-----------------------------------------------------------------

def countVals(dice):

    totals = [0, 0, 0, 0, 0]
    for x in dice:
        if x == 1:
            totals = totals[1] + 1
        elif x == 2:
            totals = totals[2] + 1
        elif x == 3:
            totals = totals[3] + 1
        elif x == 4:
            totals = totals[4] + 1
        elif x == 5:
            totals = totals[5] + 1

            print(totals)
            return totals

#------------------------------------------------------------------

rollDice()
countVals()

Upvotes: 0

Views: 317

Answers (3)

Ron
Ron

Reputation: 1269

I think the problem is that the result returned by your rollDice function is a list of strings. The if - else statement in countVals then falls through cause for example '5' == 5 -> False. You could modify rollDice to return a list of int's instead (don't convert your ints to strings):

def rollDice():

    dice = [randint(1, 6) for _ in range(5)]
    print(dice)
    return dice

If you absolutely want rollDice to return a list of strings you could convert the strings to ints using the int method in your countVals method. Example: int('5') -> 5, or just compare strings and not ints. x == '5'

Also make sure that you are saving your totals back to the right index in you totals list (in rollDice). You could do this a little more succinctly as follows: totals[1] += 1, for example:

def countVals(dice):

    totals = [0, 0, 0, 0, 0, 0] #alternatively could be 'totals = [0]*6' :)
    for value in dice:
        totals[value - 1] += 1

    print(totals)
    return totals

(assuming rollDice has been modified to return a list of integers)

You should be able to call the methods as follows totals = countVals(rollDice()) to get your list of totals.

Upvotes: 0

wflynny
wflynny

Reputation: 18531

You could try the following:

dice = rollDice()
countVals(dice)

Also you want to fix the indentation of the print and return statements in countVals(). Currently, they only trigger if x==5. And as Salvador Dali mentions, either remove str from rollDice() or change the comparisons in countVals() to x == '1', etc.


Edit:

Here is how you may want to write your script:

def rollDice():
    dice = [randint(1, 6) for _ in range(5)]
    print(dice)
    return dice

def countVals(dice):
    totals = [0, 0, 0, 0, 0]
    for x in dice:
        # x can be, 1-5.  Index of totals can be 0-4.
        totals[x-1] += 1

        print(totals)
        return totals

dice = rollDice()
countVals(dice)

Upvotes: 0

bmoak
bmoak

Reputation: 11

I believe your error lies when you increment the count of each number,

totals = totals[1] + 1

should be,

totals[1] = totals[1] + 1

Also depending on your application you may be able to simplify your code

def countVals(dice):

    totals = [0, 0, 0, 0, 0]
    for x in dice:
        totals[x - 1] += 1
    print (totals)
    return totals

Upvotes: 1

Related Questions