2013-12-12 28 views
0

我正在寫一個使用龜作爲GUI的單詞預測程序,我無法讓代碼工作。我的問題涉及該類中的WordList類和readlist方法(接近代碼結尾)。以下是錯誤消息我得到:Python龜類和方法:TypeError:'方法'只需要2個參數(1給定)?

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

在這一行

self.list = self.stripchar() 

你必須明確地告訴該字符串必須被剝離。這將是最有可能

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

這兩行可以簡單地這樣寫

self.list = self.stripchar(self.readlist(doc)) 
相關問題