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