gpt4 book ai didi

python - 为什么会出现这个字▯?

转载 作者:太空宇宙 更新时间:2023-11-04 01:20:02 25 4
gpt4 key购买 nike

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

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

最佳答案

当您从文件中读取行时,换行符位于末尾。 pygame 的 documentation指出:

The text can only be a single line: newline characters are not rendered.

因此,您应该更改此片段:

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

关于python - 为什么会出现这个字▯?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21831492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com