Python从循环中转储变量

Python从循环中转储变量,python,Python,GetVaults模块中的Total Vaults变量在退出计数器时重置为0,而问题是否与范围相关?看起来您已经声明了全局变量,然后在方法调用中传递它们。我想知道它是否混淆了,用本地的而不是全球的。请尝试此操作,但不要传递全局变量。在全局命名空间中使用global。如果要从函数内部更新全局变量,必须在函数中使用它: def calcPayout(): global totalPayout totalPayout = totalBottles * .10 # now the glo

GetVaults模块中的Total Vaults变量在退出计数器时重置为0,而问题是否与范围相关?看起来您已经声明了全局变量,然后在方法调用中传递它们。我想知道它是否混淆了,用本地的而不是全球的。请尝试此操作,但不要传递全局变量。

在全局命名空间中使用
global
。如果要从函数内部更新全局变量,必须在函数中使用它:

def calcPayout():
    global totalPayout
    totalPayout = totalBottles * .10 # now the global gets updated
您必须对所有功能执行此操作

更明智的方法是使用return:

def calcPayout(totalBottles):
    return totalBottles * .10

totalPayout = calcPayout(totalBottles)
我无法抗拒。。。(尽管这方面仍然存在问题,但至少在其他方面不起作用的情况下,它可能会起作用)

class瓶:
定义初始化(自):
self.total瓶=0#存储累计瓶值
self.counter=1将控制循环
self.todaybarks=0#存储一天返回的瓶子数量
self.totalPayout=0#存储totalPayout x.10的计算值
def(自我):

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

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()
global TotalVages,totalPayout,TodayVages
TotalLabels=0#存储累计瓶值
计数器=1#将控制循环
Today瓶子=0#存储一天退回的瓶子数量
totalPayout=0#存储totalPayout x.10的计算值
def main():
全球TotalVages、totalPayout、TodayVages
继续
当继续时=='y':
酒瓶(柜台)
calcPayout()
打印信息(TotalBa瓶、totalPayout)
keepGoing==raw_input('是否再次运行该程序?')
def收集瓶(计数器):
全球总瓶,今日瓶

而这是。。。几乎难以置信。。。我真的很惊讶!我不知道这是什么意思。对不起,这个问题从昨晚起就一直困扰着我。我真的很想弄明白。你会如何处理把号码弄出来。我是一个初学者,有人建议我在线提问。当,我正要发布这个:-(我实际上已经测试过了,如果你有所有的方法,比如
def-getva瓶(totalva瓶,todayva瓶,counter):…
be
def-getva瓶():global-totalva瓶,todayva瓶,counter
,它可以工作
def-getva瓶(TotalVarges,TodayVarges,counter):全局TotalVarges,TodayVarges
将导致错误,因为您将TotalVarges和TodayVarges声明为局部变量和全局变量。
SyntaxError:name'TotalVarges'是局部变量和全局变量
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()
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()
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()