Python turtle classes and methods: TypeError: 'method' takes exactly 2 arguments (1 given)?
Question: I'm writing a word prediction program using turtle as a GUI and I can't get
the code to work. My problem concerns the WordList class and the readlist
method in that class (near the end of the code). Here is the error message I
get:

    
    
    Traceback (most recent call last):
    File "C:\Python32\Projects\Practice 3.py", line 118, in <module>
    main()
    File "C:\Python32\Projects\Practice 3.py", line 115, in main
    wordlist = WordList(alpha,'greatexpectationschapter1.txt')
    File "C:\Python32\Projects\Practice 3.py", line 89, in __init__
    self.list = self.stripchar()
    TypeError: stripchar() takes exactly 2 arguments (1 given)
    

and here is the code itself:

    
    
    import turtle as trt
    class Key(object):
        def __init__(self,letter,left,right,top,bottom):
            self.letter = letter
            self.left = left
            self.right = right
            self.top = top
            self.bottom = bottom
    class Board(object):
        def __init__(self):
            self.newline1 = []
            self.newline2 = []
            self.newline3 = []
            self.newline4 = []
        def makeboard(self):
            line1 = ['1','2','3','4','5','6','7','8','9','0']
            line2 = ['q','w','e','r','t','y','u','i','o','p']
            line3 = ['a','s','d','f','g','h','j','k','l']
            line4 = ['z','x','c','v','b','n','m',"'","_"]
            for i in range(len(line1)):
                self.newline1.append(Key(line1[i],0+(40*i),10+(40*i),50,0))
            for i in range(len(line2)):
                self.newline2.append(Key(line2[i],0+(40*i),10+(40*i),0,-50))
            for i in range(len(line3)):
                self.newline3.append(Key(line3[i],0+(40*i),10+(40*i),-50,-100))
            for i in range(len(line4)):
                self.newline4.append(Key(line4[i],0+(40*i),10+(40*i),-100,-150))
        def drawboard(self):
            trt.penup()
            for i in self.newline1:
                trt.goto(i.left,i.bottom)
                trt.write(i.letter, font = ("arial",25))
            for i in self.newline2:
                trt.goto(i.left,i.bottom)
                trt.write(i.letter, font = ("arial",25))
            for i in self.newline3:
                trt.goto(i.left,i.bottom)
                trt.write(i.letter, font = ("arial",25))
            for i in self.newline4:
                trt.goto(i.left,i.bottom)
                trt.write(i.letter, font = ("arial",25))
        def getletter(self):
            count = 0
            if self.newline1[0].bottom < self.y < self.newline1[0].top:
                for i in range(len(self.newline1)):
                    if self.newline1[i].left < self.x < self.newline1[i].right:
                        count+=1
                        return(self.newline1[i].letter)
            if self.newline2[0].bottom < self.y < self.newline2[0].top:
                for i in range(len(self.newline2)):
                    if self.newline2[i].left < self.x < self.newline2[i].right:
                        count+=1
                        return(self.newline2[i].letter)
            if self.newline3[0].bottom < self.y < self.newline3[0].top:
                for i in range(len(self.newline3)):
                    if self.newline3[i].left < self.x < self.newline3[i].right:
                        count+=1
                        return(self.newline3[i].letter)
            if self.newline4[0].bottom < self.y < self.newline4[0].top:
                for i in range(len(self.newline3)):
                    if self.newline4[i].left < self.x < self.newline4[i].right:
                        count+=1
                        return(self.newline4[i].letter)
    class Word(object):
        def __init__(self,word,ct):
            self.word = word
            self.ct = ct
            self.x = 0
            self.y = 0
        def __str__(self):
            return(self.word+": "+str(self.ct))
    class WordPredict(object):
        def __init__(self,board,wordlist):
            self.board = board
            self.wordlist = wordlist
            self.currentword = ""
            self.predictions = []
            self.sentence = ""
            self.enterword()
        def enterword(self):
            trt.onscreenclick(self.findletter)
        def findletter(self,x,y):
            print(x)
            print(y)
    class WordList(object):
        def __init__(self,alpha,doc):
            self.alpha = alpha
            self.list = self.readlist(doc)
            self.list = self.stripchar()
            self.wordlist = self.makewordlist()
        def stripchar(self,ls):
            newlist = []
            for x in ls:
                z = ""
                for y in x:
                    if y.lower() in self.alpha:
                        if y != "":
                            z += y.lower()
                        elif len(z) > 0:
                            newlist.append(z)
                            z = ""
            return(newlist)
        def readlist(self,doc):
            f = open(doc,'r')
            ls = []
            for line in f:
                ls.append(line.strip())
            f.close()
            ls = self.stripchar(ls)
            return(ls)
    def main():
        alpha = ['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']
        board1 = Board()
        board1.makeboard()
        wordlist = WordList(alpha,'greatexpectationschapter1.txt')
        board1.drawboard()
        word = WordPredict(board1,wordlist)
    main()
    

Is there anything I can do to fix it? Thank you for your help.


Answer: In this line

    
    
    self.list = self.stripchar()
    

you have to explicitly tell which string has to be stripped. It would be most
likely

    
    
    self.list = self.stripchar(self.list)
    

Those two lines can be simply written like this

    
    
    self.list = self.stripchar(self.readlist(doc))
    

