Reputation: 51
I'm getting a NameError: global name 'mapping' is not defined. Where am I going wrong?
class CheckFunction:
mapping = [6,2,5,5,4,5,6,3,7,6]
def newFunction(self,code):
splitting = code.split()
count = 0
for n in splitting:
count += mapping[int(n)]
return count
obj = CheckFunction()
obj.newFunction("13579")
Upvotes: 0
Views: 1023
Reputation: 86138
I would do something like this:
class CheckFunction(object):
def __init__(self):
self.mapping = [6,2,5,5,4,5,6,3,7,6]
def newFunction(self,code):
count = 0
for n in code:
count += self.mapping[int(n)]
return count
obj = CheckFunction()
obj.newFunction("13579")
Result:
>>> obj.newFunction('13579')
21
Upvotes: 2
Reputation: 34146
I think the problem is that you are using mapping
just like that. You must use it like:
self.mapping
Upvotes: 2