Python龟类和方法:TypeError:'method'需要2个参数(给定1个)?

时间:2013-12-12 02:28:06

标签: python class arguments

我正在使用turtle作为GUI编写单词预测程序,但我无法使代码生效。我的问题涉及WordList类和该类中的readlist方法(接近代码的末尾)。这是我收到的错误消息:

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)

以下是代码本身:

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()

我能做些什么来解决它吗? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

在这一行

self.list = self.stripchar()

你必须明确告诉必须剥离哪个字符串。这很可能是

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

这两行可以简单地写成

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