python函数被调用才能执行吗_python3x函数在不被调用的情况下运行
這是我的密碼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)
縮進(jìn)可能有點(diǎn)錯(cuò)誤,我是新手stackoverflow。
我在標(biāo)題中指出的問題是,無(wú)論是否被調(diào)用,這兩個(gè)函數(shù)都將運(yùn)行,但如果被調(diào)用,它們將運(yùn)行兩次。在
我把括號(hào)去掉了:
^{pr2}$
所以就是這樣dice = rollDice
會(huì)解決問題,在某種程度上,它做了一些事情,但不是我想要的。
如果我這樣做,它會(huì)輸出 debug
而不是運(yùn)行這個(gè)函數(shù),所以我被困在這里。在
如果能詳細(xì)解釋一下發(fā)生了什么事,我將不勝感激?在
更新:我錯(cuò)誤地調(diào)用了函數(shù)兩次。
我想我需要先運(yùn)行它,然后才能使用它的返回輸出,但是在代碼中使用它時(shí)卻不會(huì)這樣做。在
總結(jié)
以上是生活随笔為你收集整理的python函数被调用才能执行吗_python3x函数在不被调用的情况下运行的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php生成vcf,[宜配屋]听图阁 -
- 下一篇: python处理文件夹_python文件