开发者

Python dumps variable from loop

Total bottles variable in the getBottles module resets to 0 upon exiting the while counter <8 loop. I've also tried using return statements but to no avail. How do I get the value from totalBottles variable to pass to my other module.

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 (to开发者_如何学PythontalBottles, 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()


i can't resist... (although there are still issues with this at least it might work where the other won't)

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()


if you want to use global variables, you have to specify this in the function also :

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()

otherwise you could use return values :

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':
        totalBottles = getBottles(totalBottles, todayBottles)
        totalPayout = calcPayout (totalBottles)
        printInfo(totalBottles, totalPayout)
        keepGoing == raw_input ('Do you want to run the program again?')

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

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

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

main()


Could the problem be scope related? It seems you have declared global variables and then pass them around in your method calls. I wonder if it is confused, using a local totalBottles instead of the global one. Try this without passing around your global variables.


Using global in the global namespace does nothing. If you want to update a global from inside a function, you have to use it there:

def calcPayout():
    global totalPayout
    totalPayout = totalBottles * .10 # now the global gets updated

You have to do this to all your functions.

The much, much saner way is to use return:

def calcPayout(totalBottles):
    return totalBottles * .10

totalPayout = calcPayout(totalBottles)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜