我是靠谱客的博主 故意故事,最近开发中收集的这篇文章主要介绍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)

缩进可能有点错误,我是新手stackoverflow。

我在标题中指出的问题是,无论是否被调用,这两个函数都将运行,但如果被调用,它们将运行两次。在

我把括号去掉了:

^{pr2}$

所以就是这样dice = rollDice

会解决问题,在某种程度上,它做了一些事情,但不是我想要的。

如果我这样做,它会输出 debug

而不是运行这个函数,所以我被困在这里。在

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

更新:我错误地调用了函数两次。

我想我需要先运行它,然后才能使用它的返回输出,但是在代码中使用它时却不会这样做。在

最后

以上就是故意故事为你收集整理的python函数被调用才能执行吗_python3x函数在不被调用的情况下运行的全部内容,希望文章能够帮你解决python函数被调用才能执行吗_python3x函数在不被调用的情况下运行所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部