1
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

1
  • 1
    is that all the code? Are you sure this is not inside a class? Commented Jun 1, 2013 at 11:24

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for detecting an issue with a key line apparently missing. Good eye

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.