I'm trying to write a class containing some code i want to reuse in other projects, but I just cant seem to get it right. I'm hoping someone out there can offer a helping hand.

Here is the original code:

def Numero(name):
    'A function which converts a string to a numerical value(integer)'
    alpha = { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5,
              'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 1,
              'k': 2, 'l': 3, 'm': 4, 'n': 5, 'o': 6,
              'p': 7, 'q': 8, 'r': 9, 's': 1, 't': 2,
              'u': 3, 'v': 4, 'w': 5, 'x': 6, 'y': 7,
              'z': 8, ' ': 0}

    counter1 = 0
    counter2 = []
    
    while counter1 != len(name):
        for key, value in alpha.items():
            if key in name[counter1]:
                counter2.append(value)
        counter1 += 1
        
    tester = sum(counter2)
    if tester > 9:
        tester1 = list(str(tester))
        return RoundOne(tester1)
    else:
        return tester
        
        
    return

def RoundOne(numb):
    'first round of additions'
    numbers = { '1': 1, '2': 2, '3': 3, '4': 4, '5': 5,
                '6': 6, '7': 7, '8': 8, '9': 9, '0': 0}

    stringcount1 = 0
    stringcount2 = []
    stringcount3 = 0
    while stringcount1 != len(numb):
        for key, value in numbers.items():
            if key in numb[stringcount1]:
                stringcount2.append(value)
        stringcount1 += 1      

    stringcount3 = sum(stringcount2)
    if stringcount3 > 9:
        stringcount3a = list(str(stringcount3))
        return RoundTwo(stringcount3a)
    else:
        return stringcount3

    return

def RoundTwo(numb1):
    'second round of additions'
    numbers = { '1': 1, '2': 2, '3': 3, '4': 4, '5': 5,
                '6': 6, '7': 7, '8': 8, '9': 9, '0': 0}

    stringcounta = 0
    stringcountb = []

    while stringcounta != len(numb1):
        for key, value in numbers.items():
            if key in numb1[stringcounta]:
                stringcountb.append(value)
        stringcounta += 1

    return sum(stringcountb)

And now here is the class version which so far keeps giving a NameError, which states global variable 'value' is not defined:

class NumberCrunch:
# The Numero function is the only one that should be accessed in the class.
# The remaining functions do the grunt work if Numero cant return a small
# Enough integer value
        
    def Numero(self, name):

        alpha = { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5,
                  'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 1,
                  'k': 2, 'l': 3, 'm': 4, 'n': 5, 'o': 6,
                  'p': 7, 'q': 8, 'r': 9, 's': 1, 't': 2,
                  'u': 3, 'v': 4, 'w': 5, 'x': 6, 'y': 7,
                  'z': 8, ' ': 0}
            
        counter1 = 0
        counter2 = []
        
            
        while counter1 != len(name):
            if alpha.has_key(name[counter1]):
                counter2.append(value)
            counter1 += 1
            
        firstVal = sum(counter2)
            
        if firstVal > 9:
            secondVal = list(str(firstVal))
            return self.RoundOne(secondVal)
        else:
            return self.firstVal
        
# RoundOne() provides the first round of addition. If Numero cant provide a
# a small enough integer then it will attempt to split and then add the integer
# supplied by Numero(). This should be a private method of the class.

    def RoundOne(self, numb):

        numbers = { '1': 1, '2': 2, '3': 3, '4': 4, '5': 5,
                    '6': 6, '7': 7, '8': 8, '9': 9, '0': 0}
            
        stringcount1 = 0
        stringcount2 = []
       
            
        while stringcount1 != len(numb):
            if numbers.has_key(numb[stringcount1]):
                stringcount2.append(value)
            stringcount1 += 1      

        stringcount3 = sum(stringcount2)
            
        if stringcount3 > 9:
            stringcount4 = list(str(stringcount3))
            return RoundTwo(stringcount4)
        else:
            return stringcount3

# RoundTwo() provides the second round of additions. If RoundOne() can not
# produce the required size of integer then this final function will repeat
# the same actions as RoundOne() and will yield the final result which is then
# given back to Numero to pass back to the calling code.

    def RoundTwo(self, numb1):

        numbers = { '1': 1, '2': 2, '3': 3, '4': 4, '5': 5,
                    '6': 6, '7': 7, '8': 8, '9': 9, '0': 0}

        stringcount5 = 0
        stringcount6 = []

        while stringcount5 != len(numb1):
            if numbers.has_key(numb1[stringcount5]):
                stringcount6.append(value)
            stringcount5 += 1

        return sum(stringcount6)

Any help or pointers in the right direction would be really apreciated. I can understand the concepts of OOP i just cant seem to put them in to practise lol

Recommended Answers

All 4 Replies

I've just found that if I replace the while loops in the class with the while loops from the original code everything works as it should and the value i want is returned. Does this mean there is a problem using has_key() inside a class or have i just implemented it wrong ??

Interesting. A couple of things:

* You don't have an "__init__" method. That's not strictly necessary for every class, but most classes have them in order to initialize their data members. So this raises the question, "What kind of object is this class?"
* Another way of asking the same is, "Why do you want to turn these three functions into class methods?"
* You reference the variable "value" in line 15 (or so) of method Numero, but "value" is never assigned a value :). Hence, Python complains about the global variable -- it thinks that you are talking about some variable called "value" that was assigned outside the class.

You didn't have that problem in the original because the while loop was assigning values to the variables 'key' and 'value' every iteration.

* Can you explain concisely what these three functions are trying to accomplish? I can tell that you want to turn a string into its corresponding value, but it's unclear how the value is supposed to be related to the string.

The reason I ask is that it seems like there ought to be a more efficient way of doing what you want.

Jeff

Hi,
- As i mentioned in the earlier post you can initialize alpha and number (dictionary ) in the __init__ method.
- Also there is common set of task you do in every function you can take-out that repeating line of code and make a function.

class NumberCrunch:
    def __init__(self):
        # here you do the initialization, which will be instantiated when the object gets created.
        self.alpha = { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5,
                  'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 1,
                  'k': 2, 'l': 3, 'm': 4, 'n': 5, 'o': 6,
                  'p': 7, 'q': 8, 'r': 9, 's': 1, 't': 2,
                  'u': 3, 'v': 4, 'w': 5, 'x': 6, 'y': 7,
                  'z': 8, ' ': 0}
        self.number = { '1': 1, '2': 2, '3': 3, '4': 4, '5': 5,
                    '6': 6, '7': 7, '8': 8, '9': 9, '0': 0}

    def Numero(self, name):
        firstVal = self.countFinder(name, self.alpha)
        if firstVal > 9:
            return self.RoundOne( list(str(firstVal)) )
        else:
            return firstVal

    def RoundOne(self, numb):
        secondVal = self.countFinder(numb, self.number)
        if secondVal > 9:
            return RoundTwo( list(str(secondVal)) )
        else:
            return secondVal

    def RoundTwo(self, numbl):
        return self.countFinder(numbl, self.number)
        

    def countFinder(self, li, kwarg):
        count = 0
        countList = []
        while count != len(li):
            if kwarg.has_key( li[count] ):
                countList.append( kwarg[ li[count] ] )
            count += 1

        return sum(countList)

if __name__ == '__main__':
    a = NumberCrunch()
    print a.Numero("abcdef")

hope this helps,
kath.

thanks for the help guys. The three functions, in my simple mind, where meant to perform the task of taking a users full birth name and convert each character in that name to a number. The resulting sequence of numbers would then be added to arrive at the final single digit result between 0-9.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.