200行代码的python程序

时间: 2023-12-17 14:03:10 浏览: 26
好的,这里是一个200行左右的Python程序,它是一个简单的图书管理系统,包括添加图书、借阅图书、归还图书、查询图书等功能: ```python class Book: def __init__(self, title, author, isbn, num_copies): self.title = title self.author = author self.isbn = isbn self.num_copies = num_copies self.num_available = num_copies def __str__(self): return f"{self.title} by {self.author}" def display(self): print(f"Title: {self.title}") print(f"Author: {self.author}") print(f"ISBN: {self.isbn}") print(f"Number of Copies: {self.num_copies}") print(f"Number of Available Copies: {self.num_available}") class Library: def __init__(self): self.books = [] def add_book(self, book): self.books.append(book) def remove_book(self, book): self.books.remove(book) def find_book_by_title(self, title): for book in self.books: if book.title == title: return book return None def find_book_by_author(self, author): matching_books = [] for book in self.books: if book.author == author: matching_books.append(book) return matching_books def display_books(self): for book in self.books: book.display() class Borrower: def __init__(self, name): self.name = name self.borrowed_books = [] def borrow_book(self, book): if book.num_available == 0: print("Sorry, this book is not available for borrowing.") else: book.num_available -= 1 self.borrowed_books.append(book) def return_book(self, book): if book in self.borrowed_books: book.num_available += 1 self.borrowed_books.remove(book) else: print("You did not borrow this book from the library.") def display_borrowed_books(self): if len(self.borrowed_books) == 0: print("You have not borrowed any books from the library.") else: print("Your borrowed books:") for book in self.borrowed_books: print(book) def main(): library = Library() book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", "978-0141182636", 5) book2 = Book("To Kill a Mockingbird", "Harper Lee", "978-0446310789", 3) book3 = Book("1984", "George Orwell", "978-0451524935", 2) library.add_book(book1) library.add_book(book2) library.add_book(book3) borrower1 = Borrower("Alice") borrower2 = Borrower("Bob") while True: print() print("Welcome to the Library Management System!") print("1. Add a book") print("2. Remove a book") print("3. Find a book by title") print("4. Find books by author") print("5. Display all books") print("6. Borrow a book") print("7. Return a book") print("8. Display borrowed books") print("9. Quit") try: choice = int(input("Please enter your choice: ")) except ValueError: print("Invalid choice. Please enter a number.") continue if choice == 1: title = input("Please enter the book title: ") author = input("Please enter the book author: ") isbn = input("Please enter the book ISBN: ") num_copies = int(input("Please enter the number of copies: ")) book = Book(title, author, isbn, num_copies) library.add_book(book) print("Book added successfully.") elif choice == 2: title = input("Please enter the title of the book you want to remove: ") book = library.find_book_by_title(title) if book: library.remove_book(book) print("Book removed successfully.") else: print("Book not found.") elif choice == 3: title = input("Please enter the book title: ") book = library.find_book_by_title(title) if book: book.display() else: print("Book not found.") elif choice == 4: author = input("Please enter the author name: ") matching_books = library.find_book_by_author(author) if len(matching_books) == 0: print("No matching books found.") else: print("Matching books:") for book in matching_books: book.display() elif choice == 5: library.display_books() elif choice == 6: title = input("Please enter the title of the book you want to borrow: ") book = library.find_book_by_title(title) if book: borrower = input("Please enter your name: ") if borrower == borrower1.name: borrower1.borrow_book(book) elif borrower == borrower2.name: borrower2.borrow_book(book) else: print("Borrower not found.") else: print("Book not found.") elif choice == 7: title = input("Please enter the title of the book you want to return: ") book = library.find_book_by_title(title) if book: borrower = input("Please enter your name: ") if borrower == borrower1.name: borrower1.return_book(book) elif borrower == borrower2.name: borrower2.return_book(book) else: print("Borrower not found.") else: print("Book not found.") elif choice == 8: borrower = input("Please enter your name: ") if borrower == borrower1.name: borrower1.display_borrowed_books() elif borrower == borrower2.name: borrower2.display_borrowed_books() else: print("Borrower not found.") elif choice == 9: print("Goodbye!") break else: print("Invalid choice. Please enter a number between 1 and 9.") if __name__ == '__main__': main() ``` 这个程序使用了面向对象的编程风格,其中定义了三个类:Book、Library和Borrower。Book类表示一本书,包括书名、作者、ISBN和库存数量等属性,以及展示书籍信息的方法。Library类表示图书馆,包括添加图书、删除图书、查找图书等方法。Borrower类表示借阅者,包括借阅图书、归还图书、展示已借阅图书等方法。 在主函数中,用户可以选择不同的操作,例如添加图书、借阅图书、归还图书、查询图书等。每个操作都会调用相应的方法来完成相应的任务。程序也包含了一些错误处理,例如用户输入非法的选项时会提示用户重新输入。

相关推荐

最新推荐

recommend-type

python程序快速缩进多行代码方法总结

在本篇文章里小编给大家整理了关于python程序如何快速缩进多行代码的相关知识点,需要的朋友们学习下。
recommend-type

python实现简单的购物程序代码实例

主要介绍了python实现简单的购物程序代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

Python小游戏之300行代码实现俄罗斯方块

主要给大家介绍了关于Python小游戏之300行代码实现俄罗斯方块的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
recommend-type

python代码编写计算器小程序

主要为大家详细介绍了python代码编写计算器小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

EduCoder实践课程——Python程序设计入门答案

记:由于疫情暂时返不了校,然后学校大四毕业年级布置了在线实训的任务,我选择了实践课程Python程序设计入门。以前没有学过,可能是之前有过acm经验,感觉Python挺好入门的,把自己学习过程中的代码记录下来,一是...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。