Python从循环转储变量

2024-09-27 07:28:32 发布

您现在位置:Python中文网/ 问答频道 /正文

一旦退出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()

Tags: 模块ofthestorenumberdefcounterday
3条回答

问题是否与范围有关?似乎您已经声明了全局变量,然后在方法调用中传递它们。我想知道的是,用的是全球的,而不是全球的。在不传递全局变量的情况下尝试此操作。在

如果要使用全局变量,还必须在函数中指定:

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():
    global totalBottles, totalPayout, todayBottles
    keepGoing='y'

    while keepGoing =='y':
        getBottles (counter)
        calcPayout ()
        printInfo(totalBottles, totalPayout)
        keepGoing == raw_input ('Do you want to run the program again?')

def getBottles (counter):
    global totalBottles, todayBottles
    while counter <8:
        todayBottles = input ('Enter number of bottles returned for the day:')
        totalBottles = todayBottles + totalBottles
        counter=counter + 1

def calcPayout():
    global totalBottles, totalPayout, todayBottles
    totalPayout = totalBottles * .10

def printInfo(totalBottles, totalPayout):
    print totalBottles,('is the total bottles')
    print totalPayout, ('amount due')

main()

否则可以使用返回值:

^{pr2}$

我无法抗拒。。。(尽管这仍然存在问题,但至少在其他人无法使用的情况下,它可能会起作用)

class Bottles:
    def __init__(self):
       self.totalBottles=0 #store the accumulated bottle values
       self.counter=1 #will control the loop
       self.todayBottles=0 #store the number of bottles returned on a day
       self.totalPayout=0 #store the calculated value of totalBottles x.10

    def getBottles(self):
        while self.counter <8:
            self.todayBottles = input ('Enter number of bottles returned for the day:')
            self.totalBottles = self.todayBottles + self.totalBottles
            self.counter=self.counter + 1

    def calcPayout(self):
        self.totalPayout = self.totalBottles * .10

    def printInfo(self):
        print self.totalBottles,('is the total bottles')
        print self.totalPayout, ('amount due')

def main():
    keepGoing='y'

    while keepGoing =='y':
        b = Bottles()
        b.getBottles()
        b.calcPayout()
        b.printInfo()
        keepGoing == raw_input ('Do you want to run the program again?')

main()

相关问题 更多 >

    热门问题