The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a library tracker

Here is a simple yet functional Library Tracker concept implemented in Python. This program helps you manage books in a library, tracking which books are available, borrowed, and who borrowed them. It includes basic features like adding books, borrowing, returning, and listing books.

python
class LibraryTracker: def __init__(self): # Stores books as {book_id: {"title": str, "author": str, "borrowed_by": None or str}} self.books = {} self.next_id = 1 def add_book(self, title, author): book_id = self.next_id self.books[book_id] = {"title": title, "author": author, "borrowed_by": None} self.next_id += 1 print(f'Book added: "{title}" by {author} (ID: {book_id})') def borrow_book(self, book_id, borrower_name): if book_id not in self.books: print("Book ID not found.") return if self.books[book_id]["borrowed_by"]: print(f'Book "{self.books[book_id]["title"]}" is already borrowed by {self.books[book_id]["borrowed_by"]}.') else: self.books[book_id]["borrowed_by"] = borrower_name print(f'Book "{self.books[book_id]["title"]}" has been borrowed by {borrower_name}.') def return_book(self, book_id): if book_id not in self.books: print("Book ID not found.") return if not self.books[book_id]["borrowed_by"]: print(f'Book "{self.books[book_id]["title"]}" is not currently borrowed.') else: borrower = self.books[book_id]["borrowed_by"] self.books[book_id]["borrowed_by"] = None print(f'Book "{self.books[book_id]["title"]}" has been returned by {borrower}.') def list_books(self): if not self.books: print("No books in the library.") return print("Library books:") for book_id, info in self.books.items(): status = f"Borrowed by {info['borrowed_by']}" if info["borrowed_by"] else "Available" print(f"ID: {book_id} | Title: {info['title']} | Author: {info['author']} | Status: {status}") # Example usage: library = LibraryTracker() library.add_book("1984", "George Orwell") library.add_book("To Kill a Mockingbird", "Harper Lee") library.list_books() library.borrow_book(1, "Alice") library.borrow_book(2, "Bob") library.list_books() library.return_book(1) library.list_books()

This code provides a straightforward way to track books in a library, including their availability and who has borrowed them. You can expand it with saving/loading to files, due dates, and more complex user management as needed.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About