python oop import - NameError confusion -


i wrote code meant try approach target string selecting randomly list of chars, have problem not quite understand.

import random  class monkiesgo:      __chars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']      def __init__(self, targetstring):         __targetstring = targetstring.lower()         __targetlist = list(targetstring)         __attemptlist = []         __unmatchedindexes = [ x x in range(0, (len(targetstring)-1)) ]      def attempttosolve(self):          if len(__unmatchedindexes) == 0:             __attemptstring = ''.join(__attemptlist)             return __attemptstring, __targetstring         else:             index in __unmatchedindexes:                 __attemptlist[index] = randomchar()      def updatesolutionprogress(self):         indexcheck in __unmatchedindexes:             if __targetlist[index] == __attemptlist[index]:                 __indextoclear = __unmatchedindexes.index(index)                 del __unmatchedindexes[indexttoclear]      def __randomchar(self):         return __chars[ random.randint(0,26) ] 

when import python shell in terminal, make object follows:

from monkies import monkiesgo  mk = monkiesgo("hello") mk.attempttosolve() 

i error:

traceback (most recent call last): file "", line 1, in
file "path/to/the/file/monkies.py", line 15, in attempttosolve if len(__unmatched 0: nameerror: name '_monkiesgo__unmatched' not defined

what causing this, , why there underscore before monkiesgo?

thanks in advance.

updated to:

import random  class monkiesgo:       def __init__(self, targetstring):         self.targetstring = targetstring.lower()         self.targetlist = list(targetstring)         self.attemptlist = []         self.unmatchedindexes = [ x x in range(0, (len(targetstring)-1)) ]         self.chars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']      def attempttosolve(self):          if len(self.unmatchedindexes) == 0:             self.attemptstring = ''.join(self.attemptlist)             return self.attemptstring, self.targetstring         else:             index in self.unmatchedindexes:                 self.attemptlist[index] = randomchar()      def updatesolutionprogress(self):         indexcheck in self.unmatchedindexes:             if self.targetlist[index] == self.attemptlist[index]:                 indextoclear = self.unmatchedindexes.index(index)                 del self.unmatchedindexes[indexttoclear]      def randomchar(self):         return self.chars[ random.randint(0,26) ] 

now name error regarding randomchar..?

you not creating instance variables. in function __init__, variables such __targetstring local , defined within function. when call function attempttosolve, variable __unmatchedindices local , therefore undefined. double underscore not automatically make instance variable; perhaps that's confusion.

instead of __targetstring = whatever, should use self.__targetstring = whatever, or better yet drop underscores , use self.targetstring. creates member variable. access in member functions using same self.targetstring syntax. check out tutorial comes python.