在执行我的python代码时遇到此错误:
Error: line 20, in _readBookList
if bkID < cur_node.bkID: AttributeError: 'bookNode' object has no attribute 'bkID'
input.txt
文件包含以下数据:
100,2
101,3
102,5
代码:
class bookNode:
def __init__(self, bkID, availCount):
self.bookID = bkID
self.avCntr = availCount
self.ChkOutCntr = 0
self.left = None
self.right = None
class Tree:
def __init__(self):
self.root = None
def readBookList(self, bkID, availCount):
if self.root == None:
self.root = bookNode(bkID, availCount)
else:
self._readBookList(bkID,availCount,self.root)
def _readBookList(self,bkID, availCount, cur_node):
if bkID < cur_node.bkID:
if cur_node.left == None:
cur_node.left = bookNode(bkID, availCount)
# cur_node.left_child.parent = cur_node # set parent
else:
self._readBookList(bkID, availCount, cur_node.left)
elif bkID > cur_node.bkID:
if cur_node == None:
cur_node = bookNode(bkID,availCount)
# cur_node.right_child.parent = cur_node # set parent
else:
self._readBookList(bkID,availCount, cur_node.right)
else:
print("ID already in tree!")
def print_tree(self):
if self.root != None:
self._print_tree(self.root)
def _print_tree(self, cur_node):
if cur_node != None:
self._print_tree(cur_node.left)
print(cur_node.bkID)
self._print_tree(cur_node.right)
tree = Tree()
f = open("input.txt", "r")
for x in f:
print(x)
bookDetails = x.split(",")
tree.readBookList(bookDetails[0], bookDetails[1])
# _readBookList
f.close()
tree.print_tree()
答案 0 :(得分:0)
这是一个错字,
if bkID < cur_node.bkID:
应为:
if bkID < cur_node.bookID:
检查更新的代码:
class bookNode:
def __init__(self, bkID, availCount):
self.bookID = bkID
self.avCntr = availCount
self.ChkOutCntr = 0
self.left = None
self.right = None
class Tree:
def __init__(self):
self.root = None
def readBookList(self, bkID, availCount):
if self.root == None:
self.root = bookNode(bkID, availCount)
else:
self._readBookList(bkID,availCount,self.root)
def _readBookList(self,bkID, availCount, cur_node):
if cur_node != None :
if bkID < cur_node.bookID:
if cur_node.left == None:
cur_node.left = bookNode(bkID, availCount)
# cur_node.left_child.parent = cur_node # set parent
else:
self._readBookList(bkID, availCount, cur_node.left)
elif bkID > cur_node.bookID:
if cur_node == None:
cur_node = bookNode(bkID,availCount)
# cur_node.right_child.parent = cur_node # set parent
else:
self._readBookList(bkID,availCount, cur_node.right)
else:
print("ID already in tree!")
def print_tree(self):
if self.root != None:
self._print_tree(self.root)
def _print_tree(self, cur_node):
if cur_node != None:
self._print_tree(cur_node.left)
print(cur_node.bookID)
self._print_tree(cur_node.right)
tree = Tree()
f = open("input.txt", "r")
for x in f:
print(x)
bookDetails = x.split(",")
tree.readBookList(bookDetails[0], bookDetails[1])
# _readBookList
f.close()
tree.print_tree()
问题出在self._readBookList(bkID,availCount, cur_node.right)
行上,此处您没有检查cur_node.right
是否不是None
。我已经更新了。
答案 1 :(得分:-1)
问题在于您正在调用的名称是__init__
的参数,而不是属性。要解决此问题,您只需将*.bkID
更改为*.bookID
答案 2 :(得分:-1)
因为您拼写错误,所以cur_node
没有bkID
,实际上是bookID
。
另外,您忘记添加cur_node.right = bookNode(bkID,availCount)
此行。下面是完整的代码。
class bookNode:
def __init__(self, bkID, availCount):
self.bookID = bkID
self.avCntr = availCount
self.ChkOutCntr = 0
self.left = None
self.right = None
class Tree:
def __init__(self):
self.root = None
def readBookList(self, bkID, availCount):
if self.root == None:
self.root = bookNode(bkID, availCount)
else:
self._readBookList(bkID,availCount,self.root)
def _readBookList(self,bkID, availCount, cur_node):
if bkID < cur_node.bookID: #HERE
if cur_node.left == None:
cur_node.left = bookNode(bkID, availCount)
# cur_node.left_child.parent = cur_node # set parent
else:
self._readBookList(bkID, availCount, cur_node.left)
elif bkID > cur_node.bookID: #HERE
if cur_node == None:
cur_node = bookNode(bkID,availCount)
# cur_node.right_child.parent = cur_node # set parent
else:
cur_node.right = bookNode(bkID,availCount) #ADD THIS LINE
self._readBookList(bkID,availCount, cur_node.right)
else:
print("ID already in tree!")
def print_tree(self):
if self.root != None:
self._print_tree(self.root)
def _print_tree(self, cur_node):
if cur_node != None:
self._print_tree(cur_node.left)
print(cur_node.bookID) #HERE
self._print_tree(cur_node.right)
tree = Tree()
f = open("input.txt", "r")
for x in f:
print(x)
bookDetails = x.split(",")
tree.readBookList(bookDetails[0], bookDetails[1])
# _readBookList
f.close()
tree.print_tree()