mateus
mateus

Reputation: 51

Name error global name not defined

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

Answers (2)

Akavall
Akavall

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

Christian Tapia
Christian Tapia

Reputation: 34146

I think the problem is that you are using mapping just like that. You must use it like:

self.mapping

Upvotes: 2

Related Questions