为什么会出现这个角色?

时间:2014-02-17 14:20:46

标签: python python-3.x character

所以当我运行我的代码时会出现这个字符which,我认为这意味着有一个缺少的字符,因此无法显示。 (如果我错了,不确定是否纠正我)基本上我希望能够摆脱那个角色。以下是我运行代码时的样子:

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

1 个答案:

答案 0 :(得分:3)

当你从文件中读取行时,换行符就在最后。 pygame的documentation表示:

  

文本只能是一行:不呈现换行符。

所以,你应该改变这个片段:

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

到此:

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