python3x函数在未被调用的情况下运行

python3x函数在未被调用的情况下运行,python,function,python-3.x,Python,Function,Python 3.x,这是我的密码 code __author__ = 'Jared Reabow' __name__ = 'Assignment 2 Dice game' #Date created: 14/11/2014 #Date modified 17/11/2014 #Purpose: A game to get the highest score by rolling 5 virtual dice. import random #pre defined variables NumberOfDice

这是我的密码

code
__author__ = 'Jared Reabow'
__name__ = 'Assignment 2 Dice game'
#Date created: 14/11/2014
#Date modified 17/11/2014
#Purpose: A game to get the highest score by rolling 5 virtual dice.

import random 


#pre defined variables
NumberOfDice = 5 #this variable defined how many dice are to be rolled



def rollDice(NumberOfDice):
        dice = [] #this is the creation of an unlimited array (list), it containes the values of the 5 rolled dice.
        RunCount1 = 1 #This defines the number of times the first while loop has run
        while RunCount1 <= NumberOfDice:
                #print("this loop has run " , RunCount1 , " cycles.") #this is some debugging to make sure how many time the loop has run
                TempArrayHolder = random.randint(1,6) #This holds the random digit one at a time for each of the dice in the dice Array.
                dice.append(TempArrayHolder) #this takes the TempArrayHolder value and feeds it into the array called dice.
                RunCount1 += 1 #this counts up each time the while loop is run.
        return dice

rollDice(NumberOfDice)
dice = rollDice(NumberOfDice)
print(dice,"debug") #Debug to output dice array in order to confirm it is functioning

def countVals(dice,NumberOfDice):
    totals = [0]*6 #this is the creation of a array(list) to count the number of times, number 1-6 are rolled
    #print(dice, "debug CountVals function")
    SubRunCount = 0
    while SubRunCount < NumberOfDice:
         totals[dice[SubRunCount -1] -1] += 1 #this is the key line for totals, it takes the dice value -1(to d eal with starting at 0) and uses it
         #to define the array position in totals where 1 is then added.
         #print(totals)
         SubRunCount += 1
    return totals

countVals(dice,NumberOfDice)
totals = countVals(dice,NumberOfDice)
print("totals = ",totals)
就是这样

dice = rollDice
会解决这个问题,在某种程度上,它做了一些事情,但不是我想要的。 如果我执行上述操作,它将输出

<function rollDice at 0x00000000022ACBF8> debug
调试
而不是运行这个函数,所以我被困得很深

如果能详细解释一下发生了什么,我将不胜感激

更新:我错误地调用了该函数两次。 我想我需要先运行它,然后才能使用它返回的输出,但在代码中使用时,它不会运行。

您确实调用了
rollDice()
两次:

rollDice(NumberOfDice)
dice = rollDice(NumberOfDice)
第一次忽略返回值时,您可以删除该调用

您可以对countVals执行相同的操作:

countVals(dice,NumberOfDice)
totals = countVals(dice,NumberOfDice)

同样,这是两个调用,不是一个,第一个忽略返回值;只需将这些行全部删除。

你在哪里读到的?第一个版本使用一个参数
NumberOfDice
调用
rollDice
,并将调用结果(
return
value)分配给名称
dice
;第二个只是将函数本身分配给名称
dice
。因此,您所说的不是countVals(dice,NumberOfDice)totals=countVals(dice,NumberOfDice),而是totals=countVals(骰子,NumberOfDice@JaredReabow:完全正确。否则您将调用函数并忽略结果,然后调用函数并存储结果。这只是代码不需要做的额外工作。
countVals(dice,NumberOfDice)
totals = countVals(dice,NumberOfDice)