一旦退出while counter<;8循环,getbalts模块中的Total balts变量将重置为0。我也尝试过使用返回语句,但是没有用。如何从totalbalts变量中获取值以传递给另一个模块。在
global totalBottles, totalPayout, todayBottles
totalBottles=0 #store the accumulated bottle values
counter=1 #will control the loop
todayBottles=0 #store the number of bottles returned on a day
totalPayout=0 #store the calculated value of totalBottles x.10
def main():
keepGoing='y'
while keepGoing =='y':
getBottles (totalBottles, todayBottles, counter)
calcPayout (totalBottles, totalPayout)
printInfo(totalBottles, totalPayout)
keepGoing == raw_input ('Do you want to run the program again?')
def getBottles (totalBottles, todayBottles, counter):
while counter <8:
todayBottles = input ('Enter number of bottles returned for the day:')
totalBottles = todayBottles + totalBottles
counter=counter + 1
def calcPayout(totalBottles, totalPayout):
totalPayout = totalBottles * .10
def printInfo(totalBottles, totalPayout):
print totalBottles,('is the total bottles')
print totalPayout, ('amount due')
main()
问题是否与范围有关?似乎您已经声明了全局变量,然后在方法调用中传递它们。我想知道的是,用的是全球的,而不是全球的。在不传递全局变量的情况下尝试此操作。在
如果要使用全局变量,还必须在函数中指定:
否则可以使用返回值:
^{pr2}$我无法抗拒。。。(尽管这仍然存在问题,但至少在其他人无法使用的情况下,它可能会起作用)
相关问题 更多 >
编程相关推荐