AttributeError: ‘str’ object has no attribute ‘ID’ in python

I have developed a library management system, but i am facing an error right now. Which is after paying the late charge as a borrower and try to borrow an item from the library right away causes an AttributeError: ‘str’ object has no attribute ‘ID’

However, if i kill the terminal and re-enter the program and try to borrow an item again as the same user earlier, it works without any program.

Basic info: Binary search tree is used and the data comes from the designated csv file.

def search_by_id(self, item_id):
        def inorder_search(node):
            if node is None:
                return None
            if node.library_item.ID == item_id:
                return node.library_item
            left_result = inorder_search(node.left)
            if left_result:
                return left_result
            return inorder_search(node.right)
        return inorder_search(self.root)

The source code above is the function that shows error and the source code below is the binary search tree class i wrote to search for items in the library

class LibraryTreeNode:
    def __init__(self, library_item):
        self.library_item = library_item
        self.left = None
        self.right = None

class LibraryTree:
    def __init__(self):
        self.root = None
    
    #Insert library item into the BST
    def insert(self, library_item):
        if not self.root:
            self.root = LibraryTreeNode(library_item)
        else:
            self._insert(self.root, library_item)
    
    #Helper method to recursively insert library item into the BST
    def _insert(self, node, library_item):
        if library_item.ID < node.library_item.ID:
            if node.left is None:
                node.left = LibraryTreeNode(library_item)
            else:
                self._insert(node.left, library_item)
        else:
            if node.right is None:
                node.right = LibraryTreeNode(library_item)
            else:
                self._insert(node.right, library_item)
    
    #Search for library item by ID(ISSN or ISBN) in the BST
    def search(self, ID):
        return self._search(self.root, ID)
    
    #Helper method to recursively search for library items by ID(ISSN or ISBN) in the BST and return the node(ID) if found
    def _search(self, node, ID):
        if node is None or node.library_item.ID == ID:
            return node
        if ID < node.library_item.ID:
            return self._search(node.left, ID)
        return self._search(node.right, ID)
    
    def search_by_title(self, title):
        return self._search_by_title(self.root, title)

    def _search_by_title(self, node, title):
        if node is None:
            return None
        if node.library_item.title == title:
            return node.library_item
        left_search = self._search_by_title(node.left, title)
        if left_search is not None:
            return left_search
        return self._search_by_title(node.right, title)
    
    def search_by_id(self, item_id):
        def inorder_search(node):
            if node is None:
                return None
            if node.library_item.ID == item_id:
                return node.library_item
            left_result = inorder_search(node.left)
            if left_result:
                return left_result
            return inorder_search(node.right)
        return inorder_search(self.root)

    def delete(self, library_item):
        if self.root is None:
            print("The tree is empty.")
        else:
            self.root = self._delete(self.root, library_item)

    def _delete(self, current_node, node):
        if current_node is None:
            print("Cannot locate the node to delete.")
            return current_node

        if node.ID < current_node.library_item.ID:
            current_node.left = self._delete(current_node.left, node)
        elif node.ID > current_node.library_item.ID:
            current_node.right = self._delete(current_node.right, node)
        else:
            if current_node.left is None:
                return current_node.right
            elif current_node.right is None:
                return current_node.left

            temp = self._min_value_node(current_node.right)
            current_node.library_item = temp.library_item
            current_node.right = self._delete(current_node.right, temp)

        return current_node
    
    #Private helper method that helps to locate the node with the minimum value in the BST, used during the deletion process
    def _min_value_node(self, node):
        current = node
        while current.left is not None:
            current = current.left
        return current
    
    #Returns an iterator for the binary search tree
    def __iter__(self):
        return self.in_order_traversal(self.root)
    
    #Performs an inorder traversal of the BST, which visits the nodes in ascending order
    #Left child node > Parent node(Root) > Right child node
    def in_order_traversal(self, node=None):
        if node is None:
            return
        yield from self.in_order_traversal(node.left)
        yield node.library_item
        yield from self.in_order_traversal(node.right)

New contributor

Aiden Wenbin Choi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật