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
idanshmu
  • 5,061
  • 6
  • 46
  • 92
m2rt
  • 157
  • 2
  • 12
  • 1
    This code doesn't produce that error. What's your current code? – Blender Mar 20 '14 at 07: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. – Anvesh Mar 20 '14 at 07:55

3 Answers3

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.

sshashank124
  • 31,495
  • 9
  • 67
  • 76
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()
mikhdm
  • 109
  • 2
  • 12
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?

Anvesh
  • 607
  • 1
  • 5
  • 19