fork download
  1. class Book:
  2. def __init__(self, title, author, isbn):
  3. self.title = title
  4. self.author = author
  5. self.isbn = isbn # isbn means the International Standard Book Number
  6. self.borrowed = False
  7. self.published = False
  8.  
  9. def mark_as_borrowed(self):
  10. self.borrowed = True
  11.  
  12. def mark_as_returned(self):
  13. self.borrowed = False
  14.  
  15. def __str__(self):
  16. return f"{self.title} by {self.author} (ISBN: {self.isbn})"
  17.  
  18.  
  19. class Library:
  20. def __init__(self):
  21. self.books = []
  22.  
  23. def add_book(self, book):
  24. self.books.append(book)
  25.  
  26. def remove_book(self, isbn):
  27. self.books = [book for book in self.books if book.isbn != isbn]
  28.  
  29. def search_by_title(self, title):
  30. return [book for book in self.books if title.lower() in book.title.lower()]
  31.  
  32. def search_by_author(self, author):
  33. return [book for book in self.books if author.lower() in book.author.lower()]
  34.  
  35. def display_books(self):
  36. for book in self.books:
  37. print(book)
  38.  
  39. def mark_book_as_borrowed(self, isbn):
  40. for book in self.books:
  41. if book.isbn == isbn:
  42. book.mark_as_borrowed()
  43. break
  44.  
  45.  
  46. def main():
  47. library = Library()
  48. while True:
  49. print("Welcome to my library!")
  50. print("1. Add Book")
  51. print("2. Remove Book")
  52. print("3. Search by Title")
  53. print("4. Search by Author")
  54. print("5. Display Books")
  55. print("6. Mark Book as Borrowed")
  56. print("7. quit")
  57. choice = input("Enter your choice: ")
  58. if choice == "1":
  59. title = input("enter the title of the book: ")
  60. author = input("enter the author of the book: ")
  61. isbn = input("enter the ISBN of the book: ")
  62. book = Book(title, author, isbn)
  63. print("Book added successfully")
  64. elif choice == "2":
  65. isbn = input("enter the ISBN of the book you want to remove: ")
  66. library.remove_book(isbn)
  67. print("Book removed successfully")
  68. elif choice == "3":
  69. title = input("enter the title of the book: ")
  70. result = library.search_by_title(title)
  71. if result:
  72. for book in result:
  73. print(book)
  74. else:
  75. print("Book not found")
  76. elif choice == "4":
  77. author = input("enter the author of the book: ")
  78. result = library.search_by_author(author)
  79. if result:
  80. for book in result:
  81. print(book)
  82. else:
  83. print("Author not found")
  84. elif choice == "5":
  85. library.display_books()
  86. elif choice == "6":
  87. isbn = input("enter the ISBN of the book to mark as borrowed: ")
  88. library.mark_book_as_borrowed(isbn)
  89. print("Book marked as borrowed successfully")
  90. elif choice == "7":
  91. print("Thank you for using my library!")
  92. break
  93. else:
  94. print("Invalid choice")
  95.  
  96.  
  97. if __name__ == "__main__":
  98. main()
Success #stdin #stdout 0.03s 25584KB
stdin
Standard input is empty
stdout
class Book:
    def __init__(self, title, author, isbn):
        self.title = title
        self.author = author
        self.isbn = isbn             # isbn means the International Standard Book Number
        self.borrowed = False
        self.published = False

    def mark_as_borrowed(self):
        self.borrowed = True

    def mark_as_returned(self):
        self.borrowed = False

    def __str__(self):
        return f"{self.title} by {self.author} (ISBN: {self.isbn})"


class Library:
    def __init__(self):
        self.books = []

    def add_book(self, book):
        self.books.append(book)

    def remove_book(self, isbn):
        self.books = [book for book in self.books if book.isbn != isbn]

    def search_by_title(self, title):
        return [book for book in self.books if title.lower() in book.title.lower()]

    def search_by_author(self, author):
        return [book for book in self.books if author.lower() in book.author.lower()]

    def display_books(self):
        for book in self.books:
            print(book)

    def mark_book_as_borrowed(self, isbn):
        for book in self.books:
            if book.isbn == isbn:
                book.mark_as_borrowed()
                break


def main():
    library = Library()
    while True:
        print("Welcome to my library!")
        print("1. Add Book")
        print("2. Remove Book")
        print("3. Search by Title")
        print("4. Search by Author")
        print("5. Display Books")
        print("6. Mark Book as Borrowed")
        print("7. quit")
        choice = input("Enter your choice: ")
        if choice == "1":
            title = input("enter the title of the book: ")
            author = input("enter the author of the book: ")
            isbn = input("enter the ISBN of the book: ")
            book = Book(title, author, isbn)
            print("Book added successfully")
        elif choice == "2":
            isbn = input("enter the ISBN of the book you want to remove: ")
            library.remove_book(isbn)
            print("Book removed successfully")
        elif choice == "3":
            title = input("enter the title of the book: ")
            result = library.search_by_title(title)
            if result:
                for book in result:
                    print(book)
            else:
                print("Book not found")
        elif choice == "4":
            author = input("enter the author of the book: ")
            result = library.search_by_author(author)
            if result:
                for book in result:
                    print(book)
            else:
                print("Author not found")
        elif choice == "5":
            library.display_books()
        elif choice == "6":
            isbn = input("enter the ISBN of the book to mark as borrowed: ")
            library.mark_book_as_borrowed(isbn)
            print("Book marked as borrowed successfully")
        elif choice == "7":
            print("Thank you for using my library!")
            break
        else:
            print("Invalid choice")


if __name__ == "__main__":
    main()