简体   繁体   English

在另一个函数中使用一个函数的变量(未填充参数“ dice”)

[英]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. 您好,我试图创建一个游戏,其中计算机在1到6之间生成5个随机数。但是我的问题是,我创建了一个列表,该列表将根据出现的数字在其相应部分中获得“ 1”。 eg 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 例如,如果计算机生成31534,则列表需要显示[1,0,2,1,1,0](因为两个3填充了3槽中的2),因此仅显示5个随机数,而没有其他显示

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

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

You could try the following: 您可以尝试以下方法:

dice = rollDice()
countVals(dice)

Also you want to fix the indentation of the print and return statements in countVals() . 另外,您还想在countVals()修复printreturn语句的countVals() Currently, they only trigger if x==5 . 当前,它们仅在x==5触发。 And as Salvador Dali mentions, either remove str from rollDice() or change the comparisons in countVals() to x == '1' , etc. 和作为达利提到,或者删除strrollDice()或更改比较countVals()x == '1'


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)

I think the problem is that the result returned by your rollDice function is a list of strings. 我认为问题在于rollDice函数返回的结果是字符串列表。 The if - else statement in countVals then falls through cause for example '5' == 5 -> False . 然后,countVals中的if-else语句会陷入原因,例如'5' == 5 -> False You could modify rollDice to return a list of int's instead (don't convert your ints to strings): 您可以修改rollDice以返回一个int的列表(不要将int转换为字符串):

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. 如果您绝对希望rollDice返回字符串列表,则可以使用countVals方法中的int方法将字符串转换为int Example: int('5') -> 5 , or just compare strings and not ints. 例如: int('5') -> 5 ,或者只是比较字符串而不是ints。 x == '5'

Also make sure that you are saving your totals back to the right index in you totals list (in rollDice). 还要确保将总计保存回总计列表中的正确索引(在rollDice中)。 You could do this a little more succinctly as follows: totals[1] += 1 , for example: 您可以更简洁地执行此操作,如下所示: totals[1] += 1 ,例如:

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) (假设rollDice已修改为返回整数列表)

You should be able to call the methods as follows totals = countVals(rollDice()) to get your list of totals. 您应该能够如下调用方法totals = countVals(rollDice())以获得总计列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM