我已经和这个战斗了三个小时了。在
ETA-本应提及这一点,但就本类而言,不允许使用全局变量。
在函数main()中,我希望在第一次运行整个函数时运行函数firstPass。firstPass函数初始化一对变量,并打印一些不感兴趣的信息(如果不是第一次看到的话)。在
我正式拥有:#Initialize variables that need to run before we can do any work here
count = 0
def firstPass():
x = 5
y = 2
print "Some stuff"
count = count + 1
print "Count in firstPass is",count
return count,x,y
def main ():
print "Count in main is",count
if count == 0:
firstPass()
else:
#Do a whole bunch of other stuff that is NOT supposed to happen on run #1
#because the other stuff uses user-modified x and y values, and resetting
#to start value would just be self-defeating.
main()
这将在第一次传递时正确返回,但在随后的传递中,将返回:
^{pr2}$
这对于我的用户在其他函数中修改的x和y值也是一个问题。虽然我没有在这里修改它们,但我确实包含了它们,因为我需要在以后的代码中在函数之间传递多个值,但是为了这个例子,谁还想看这些呢。。。在
我的印象是return [variable]
将变量的当前值(即当前函数中的变量)值传递回其他后续函数。不是我的理解错了,就是我做错了。在