Python基础文章系列

Python 基础 (一)Python基本认识与环境搭建Python 基础 (一)Python基本认识与环境搭建
Python 基础 (二)Python变量与基本数据类型Python 基础 (二)Python变量与基本数据类型
Python 基础 (三)Python基本语句与基本运算Python 基础 (三)Python基本语句与基本运算
Python 基础 (四)Python函数Python 基础 (四)Python函数
Python 基础 (五)Python包与模块Python 基础 (五)Python包与模块
Python 基础 (六)Python的文件模块Python 基础 (六)Python的文件模块
Python 基础 (七)Python的异常处理机制Python 基础 (七)Python的异常处理机制
Python 基础 (八)Python的类与对象Python 基础 (八)Python的类与对象
Python 基础 (九)Python的内置模块Python 基础 (九)Python的内置模块
Python 基础 (十)Python实现简单的图书管理系统Python 基础 (十)Python实现简单的图书管理系统

4.10 简单练习图书管理系统

简单的图书管理系统的实现,主要功能如下:

  1. 定义了一个 Library 类,用于管理图书馆中的书籍。
  2. 提供了添加书籍、删除书籍、查找书籍、修改书籍、显示所有书籍、保存书籍、加载书籍和排序书籍等功能。
  3. 通过文件操作,可以将书籍信息保存到名为 “book.txt” 的文本文件中,以及从该文件加载书籍信息。
  4. 提供了一个命令行菜单,允许用户选择不同的操作,如添加、删除、查找、修改、显示、保存、加载和排序书籍。
  5. 使用了 Book 类来表示书籍的信息,包括书名、作者、价格、出版社、出版日期、评分、评论数和链接。
    用户可以通过输入相应的操作序号来执行不同的操作,从而管理图书馆中的书籍。
  • 图书类
# ---encoding:utf-8---# @Time: 2023/9/3 19:30# @Author: Darwin_Bossen# @Email :3139066125@qq.com# @Site:书籍类# @File: Book.pyclass Book:# 书籍类def __init__(self, name, author, price, publish, date, score, comment, url):self.name = nameself.author = authorself.price = priceself.publish = publishself.date = dateself.score = scoreself.comment = commentself.url = url# 重写str方法def __str__(self):return "书名:" + self.name + "\n作者:" + self.author + "\n价格:" + self.price + "\n出版社:" + self.publish + "\n出版日期:" + self.date + "\n评分:" + self.score + "\n评论数:" + self.comment + "\n链接:" + self.url
  • Library类
# ---encoding:utf-8---# @Time: 2023/9/3 19:32# @Author: Darwin_Bossen# @Email :3139066125@qq.com# @Site: 图书类# @File: Library.pyfrom Book import Book# 图书馆类class Library:def __init__(self):self.book_list = []def __str__(self):return str(len(self.book_list)) + "本书"# 显示所有书籍def showBook(self):for book in self.book_list:print(book)print("--------------------------------------------------")# 添加书籍def addBook(self, book):self.book_list.append(book)# 删除书籍def delBook(self, book):if book in self.book_list:self.book_list.remove(book)print("删除成功")else:print("删除失败")# 查找书籍def findBook(self, name):for book in self.book_list:if book.name == name:print(book)return bookprint("查无此书")return None# 修改书籍def modifyBook(self, book):if book in self.book_list:self.book_list.remove(book)self.book_list.append(book)print("修改成功")else:print("修改失败")# 保存书籍def saveBook(self):with open("book.txt", "w", encoding="utf-8") as f:for book in self.book_list:f.write(book.name + "," + book.author + "," + book.price + "," + book.publish + "," + book.date + "," + book.score + "," + book.comment + "," + book.url + "\n")print("保存成功")# 加载书籍def loadBook(self):with open("book.txt", "r", encoding="utf-8") as f:while True:line = f.readline()if line == "":breakbook = line.split(",")self.book_list.append(book)print("加载成功")# 排序def sortBook(self):self.book_list.sort(key=lambda book:book.score, reverse=True)print("排序成功")if __name__ == '__main__':library = Library()while True:print("1.添加书籍")print("2.删除书籍")print("3.查找书籍")print("4.修改书籍")print("5.显示所有书籍")print("6.保存书籍")print("7.加载书籍")print("8.排序")print("0.退出")num = input("请输入操作序号:")if num == "1":name = input("请输入书名:")author = input("请输入作者:")price = input("请输入价格:")publish = input("请输入出版社:")date = input("请输入出版日期:")score = input("请输入评分:")comment = input("请输入评论数:")url = input("请输入链接:")book = Book(name, author, price, publish, date, score, comment, url)library.addBook(book)elif num == "2":name = input("请输入书名:")book = library.findBook(name)library.delBook(book)elif num == "3":name = input("请输入书名:")library.findBook(name)elif num == "4":name = input("请输入书名:")book = library.findBook(name)if book != None:author = input("请输入作者:")price = input("请输入价格:")publish = input("请输入出版社:")date = input("请输入出版日期:")score = input("请输入评分:")comment = input("请输入评论数:")url = input("请输入链接:")book = Book(name, author, price, publish, date, score, comment, url)library.modifyBook(book)elif num == "5":library.showBook()elif num == "6":library.saveBook()elif num == "7":library.loadBook()elif num == "8":library.sortBook()elif num == "0":break
  • 效果


到此我们Python的基础知识就完了,下面Python的进阶知识,线程