赞
踩
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实现简单的图书管理系统 |
简单的图书管理系统的实现,主要功能如下:
Library
类,用于管理图书馆中的书籍。Book
类来表示书籍的信息,包括书名、作者、价格、出版社、出版日期、评分、评论数和链接。# ---encoding:utf-8--- # @Time : 2023/9/3 19:30 # @Author : Darwin_Bossen # @Email :3139066125@qq.com # @Site : 书籍类 # @File : Book.py class Book: # 书籍类 def __init__(self, name, author, price, publish, date, score, comment, url): self.name = name self.author = author self.price = price self.publish = publish self.date = date self.score = score self.comment = comment self.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
# ---encoding:utf-8--- # @Time : 2023/9/3 19:32 # @Author : Darwin_Bossen # @Email :3139066125@qq.com # @Site : 图书类 # @File : Library.py from 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 book print("查无此书") 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 == "": break book = 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的进阶知识,线程
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。