1
from collections import Counter

class Runlength:
    def __init__(self):
        self.str = 0 

    def returner(self,str):
        self.str = str 
        self.__str = ','.join(str(n) for n in self.__str)
        self.__str = self.__str[::-1]
        self.__str = self.__str.replace(',', '')
        return self.__str
    def final(self,num):
        self.num = num 
        k = []
        c = Counter(self.num).most_common()
        for x in c:
        k += x     
        return k
math = Runlength() 

def Main():
a = "aabbcc"
b = math.returner(a)
c = math.final(b)
print(c)
Main()

The program takes a word as input and gives the occurrence of each repeating character and outputs that number along with a single character of the repeating sequence.

I cant figure it out, why this doesn't work. I get this error:

NameError: global name 'returner' is not defined
2
  • 1
    This code doesn't produce that error. What's your current code? Commented Mar 20, 2014 at 7:47
  • As math is not declared in the function scope and it is not declared as a 'global' the 'Main()' coundnt refer to the 'math' object. Commented Mar 20, 2014 at 7:55

3 Answers 3

1

The problem is that in Main() you are not accessing the global (outside the scope of the Main() method) math variable. Instead try initializing your math inside the Main() function

This lets the method know that it should use the global math variable instead of trying to look for a non-existent local one.

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

1 Comment

Nope. You only need to use global if you want to rebind the name, not just access it.
1

I got this error with your code:

self.__str = ','.join(str(n) for n in self.__str)
AttributeError: Runlength instance has no attribute '_Runlength__str'

Maybe you mean:

self.__str = ','.join(str(n) for n in self.str

And choose input argument for returner() method as str_ not str, cause str -- is the name of python built-in type, so better to not choose variable names with built-in type names. So after this changes I got this output:

['a', 2, 'c', 2, 'b', 2]

So my python version is 2.7.3 and error you've got does not appear with my python version. What python version you use to compile your code? If this python3 it works fine too.So try this code, it works fine for me:

from collections import Counter

class Runlength:
    def __init__(self):
        self.str = 0

    def returner(self,str_):
        self.string = str_
        self.__str = ','.join(str(n) for n in self.string)
        self.__str = self.__str[::-1]
        self.__str = self.__str.replace(',', '')
        return self.__str
    def final(self,num):
        self.num = num
        k = []
        c = Counter(self.num).most_common()
        for x in c:
            k += x
        return k
math = Runlength()

def Main():
    a = "aabbcc"
    b = math.returner(a)
    c = math.final(b)
    print(c)
Main()

Comments

0
def Main():
   math = Runlength() 
   a = "aabbcc"
   b = math.returner(a)
   c = math.final(b)
   print(c)
Main()

This should work fine..

But I observed that the object can even be accessed if it is not declared as global. Is their any explantion for it in the above scenario?

Comments

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.