已经被困在这个问题上很长时间了,是时候寻求帮助了。几乎所有的东西都有用。它读取文件并输入数据等等。。程序没有给我任何输出,最后一个if语句有问题。。最后三排是
root1.put(word1)
if root.exists(word1):
print(word1, end = " ")
它将值放入树中。然后它会检查第一棵树,如果值“word1”已经在那棵树中了,如果是真的,它会打印出单词,如果不存在的话。这个程序只给我一个空输出。有没有可能有人看到这个问题?另一个类文件看起来几乎相同,只是没有对所有变量/para使用1。。在
类文件(root1)
^{pr2}$程序文件
#first tree
root = Bintree("root")
with open("word3.txt", "r", encoding = "utf-8") as file:
for row in file:
word = row.strip()
checklist = root.exists(word)
if checklist == word:
pass
else:
root.put(word)
#second tree
root1 = BintreeEN("root1")
with open('engelska.txt','r', encoding = "utf-8") as f:
for row in f:
onerow = row.split()
for rowz in onerow:
word1 = rowz.strip()
#HERE IT something thats wrong...
if root1.exists(word1):
pass
else:
root1.put(word1)
if root.exists(word1): #Check if value is in the first tree
print(word1, end = " ")
您的
exists()
方法返回一个真值,总是。您的.put()
永远不会被调用,因为第一个if
始终为真:如果树中不存在值,
^{pr2}$.exists()
方法返回(None, None)
,并且非空元组为始终True:只返回而不是元组:
或者,通过使用}立即退出函数的事实:
and
的短路特性和{通过此更改,您的二叉树可以正常工作:
相关问题 更多 >
编程相关推荐