2014-02-17 279 views
2

因此,當我運行我的代碼時出現此字符,我認爲這意味着存在缺少的字符,因此無法顯示。 (不知道糾正我,如果我錯了)基本上,我希望能夠擺脫那個角色。這是個什麼樣子,當我運行我的代碼,如:這個字符爲什麼會出現?

enter image description here

但是在空閒的後端,當我點擊框爲它的一個要顯示向上頂不登記而像這樣在怠速:

enter image description here

它爲什麼會出現在屏幕上,如果不會出現閒置呢? 另外我怎樣才能擺脫主屏幕上的▯字符?

Here is my full code.

以下是我認爲問題的關鍵在於段。 (不過,我一直沒能解決的問題)

我班樹比較,找出句子及其頻繁使用:

class Branch(): 
    def __init__(self, value): 
     self.left = None 
     self.right = None 
     self.value = value 
     self.frequency = 1 

    def incFreq(self): 
     self.frequency = self.frequency + 1 

    def freq(self): 
     return self.frequency 

class Tree():  

    highest = [] 

    def __init__(self): 
     self.root = None 
     self.found = False   

    def findHighest(self): 
     from operator import itemgetter, attrgetter 
     self.highest = [] 
     self.inorder(self.root) 
     self.highest = sorted(self.highest, key=itemgetter(1), reverse=True)   
     return self.highest 

    #lessThan function needed to compare strings 
    def lessThan(self, a, b):  
     if len(a) < len(b): 
      loopCount = len(a) 
     else: 
      loopCount = len(b)   
     for pos in range(0, loopCount): 
      if a[pos] > b[pos]: 
       return False   
     return True 

    def outputTree(self): 
     self.inorder(self.root) 

    def insert(self, value): 
     #increment freq if already exists, else insert 
     if not self.exists(value):    
      self.root = self.insertAtBranch(self.root, value) 

    def exists(self, value): 
     #set the class variable found to False to assume it is not there  
     self.found = False 
     self.findAtBranch(self.root, value) 
     return self.found 

    #Used to fine a value in a tree 
    def findAtBranch(self, branch, value):   
     if branch == None: 
      pass 
     else: 
      #print ("[" + branch.value + "][" + value + "]") # Error checking 
      if branch.value == value: 
       self.found = True 
       #print("found " + value) 
       branch.incFreq() 
       #print(branch.freq()) 
      else: 
       self.findAtBranch(branch.left, value) 
       self.findAtBranch(branch.right, value)   

    def insertAtBranch(self, branch, value): 
     if branch == None: 
      return Branch(value) 
     else: 
      if self.lessThan(branch.value, value): 
       branch.right = self.insertAtBranch(branch.right, value)    
      else: 
       branch.left = self.insertAtBranch(branch.left, value) 
      return branch 

    def inorder(self, branch): 
     if branch == None: return 
     self.highest.append((branch.value, branch.freq())) 
     #print (branch.value) 
     #print (branch.freq()) 
     #print(self.highest[0]) 
     self.inorder(branch.left) 
     self.inorder(branch.right) 

這是我用的樹,並通過句子中使用在不同的功能:

def getPhrases(self, numToReturn): 

    topPhrases = [] 
    phrasesTree = Tree() 

    #load tree with phrases from phrase text file 
    file = open('setPhrases.txt', 'r') 
    for line in file:  
     phrasesTree.insert(line) 

    #create a list of the top n of phrases to return 
    val = 0 
    for phrase in phrasesTree.findHighest(): 
     if val < numToReturn: 
      topPhrases.append(phrase) 
     val = val + 1 

    return topPhrases 

這是我使用的句子能夠在屏幕上顯示出來:

def createPhrases(self): 

    print("createPhrases") 
    self.deletePanes() 

    self.show_keyboard = False 
    self.show_words = False 
    self.show_phrases = True 
    self.show_terminal = True 

    words = self.getPhrases(10) 
    for word, count in words: 
     self.addPane("{}".format(word, count), WORDS) 
    self.addPane("Boxes", PHRASE) 
    self.addPane("Keyboard", PHRASE) 
    self.addPane("OK", PHRASE) 
    self.drawPanes() 

回答

3

當您從文件中讀取行時,換行符處於末尾。 pygame的documentation指出:

文本只能是一行:不顯示換行符。

所以,你應該改變這種片段:

​​

這樣:

file = open('setPhrases.txt', 'r') 
for line in file:  
    phrasesTree.insert(line.strip()) 
+0

呀謝謝,非常感謝。它的工作,並感謝你解釋的答案以及。 :) – PythonNovice

相關問題