user2443048
user2443048

Reputation: 25

Global Variable giving error

g=0
def smooth(self, a, b):
    k=0
    c = self.name[a]
    d = self.name[b]
    e,f=c,d
    while(e.get_p()!=f.get_p() and e.get_p()!=None and f.get_p()!=None):
        k+=1
        e=e.get_p()
        f=f.get_p()
    if(e.get_p==None and f.get_p()!=None):
        global g
        g+=1
        d=d.get_p()
        return self.smooth(a,d.name)
    return(k,g)

Ignore the functions called but in the if statement it is not updating value of g and giving an error global name 'g' is not defined on calling with a value.Please Help

Upvotes: 1

Views: 175

Answers (1)

Crowman
Crowman

Reputation: 25908

In this code:

g=0
def smooth(self, a, b):

smooth looks like a class instance method, and that g therefore looks like a class variable, not a global one, so the global keyword won't work. Try referring to it as MyClass.g instead (where 'MyClass' is the actual name of your class), or __class__.g.

Upvotes: 3

Related Questions