[英]AttributeError: 'class' object has no attribute
I am running into this error while executing my python code:我在执行 python 代码时遇到了这个错误:
Error: line 20, in _readBookList
if bkID < cur_node.bkID: AttributeError: 'bookNode' object has no attribute 'bkID'
The input.txt
file has the following data: input.txt
文件包含以下数据:
100,2
101,3
102,5
Code:代码:
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()
It is a typo error,这是一个拼写错误,
if bkID < cur_node.bkID:
should be:应该:
if bkID < cur_node.bookID:
Check the updated code:检查更新的代码:
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()
The problem is with the line, self._readBookList(bkID,availCount, cur_node.right)
, here you are not checking if cur_node.right
is not None
or not.问题在于
self._readBookList(bkID,availCount, cur_node.right)
行,在这里您没有检查cur_node.right
是否为None
。 I have updated it.我已经更新了它。
The problem is that the name you are calling is an argument to the __init__
, not an attribute.问题是您调用的名称是
__init__
的参数,而不是属性。 to fix this you just need to change *.bkID
to *.bookID
要解决此问题,您只需将
*.bkID
更改为*.bookID
Because you spelled it wrong cur_node
does not have bkID
it is actually bookID
.因为您拼写错误
cur_node
没有bkID
它实际上是bookID
。 Also, you forgot to add cur_node.right = bookNode(bkID,availCount)
this line.此外,您忘记添加
cur_node.right = bookNode(bkID,availCount)
这一行。 Below is the complete code.下面是完整的代码。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.