classBook: def__init__(self, title, author, year): self.title = title self.author = author self.year = year self.borrowed = False
defborrow(self): if self.borrowed: print("This book is already borrowed.") else: self.borrowed = True print("Book borrowed successfully:", self.title)
defreturn_book(self): ifnot self.borrowed: print("This book is not borrowed.") else: self.borrowed = False print("Book returned successfully:", self.title)
# Add books to the library library.add_book(book1) library.add_book(book2)
# Borrow books library.borrow_book("Python Beginner's Guide") library.borrow_book("Java Programming Practice") library.borrow_book("C++ Basics") # Book not found
# Return books library.return_book("Python Beginner's Guide") library.return_book("Java Programming Practice") library.return_book("C++ Basics") # Book not found
defborrow_book(title): for book in books: if book["title"] == title: if book["borrowed"]: print("This book is already borrowed.") else: book["borrowed"] = True print("Book borrowed successfully:", title) return print("Book not found:", title)
defreturn_book(title): for book in books: if book["title"] == title: ifnot book["borrowed"]: print("This book is not borrowed.") else: book["borrowed"] = False print("Book returned successfully:", title) return print("Book not found:", title)
# Add books to the library add_book("Python Beginner's Guide", "John Smith", 2019) add_book("Java Programming Practice", "Jane Doe", 2020)
# Borrow books borrow_book("Python Beginner's Guide") borrow_book("Java Programming Practice") borrow_book("C++ Basics") # Book not found
# Return books return_book("Python Beginner's Guide") return_book("Java Programming Practice") return_book("C++ Basics") # Book not found