(编辑:jimmy 日期: 2024/12/29 浏览:2)
本文实例为大家分享了python代码实现图书管理系统的具体代码,供大家参考,具体内容如下
图书管理系统
功能简介
书籍信息以如下格式保存在txt文本文件格式下
源代码
#-*- coding=utf8 -*- # @author:sololi # date: 2020/11/12 # 文件说明 : data import sys def print_muen(): print("----菜单提示----") print("[1]:添加图书") print("[2]:查询图书") print("[3]:删除图书") print("[4]:修改图书信息") print("[5]:显示所有图书") print("[6]:退出") def add_book(): book_id=input("请输入图书编号") book_name=input("请输入图书名称") book_loc=input("请输入图书位置") for i in books: if book_id==i["book_id"]: print("编号重复") break else: book={} book["book_id"]=book_id book["book_name"]=book_name book["book_loc"]=book_loc books.append(book) new_file(books) print("添加成功") def find_book(): jg=0 while True: book_name=input("输入要找的书名") for i in books: if book_name==i["book_name"]: print(i) jg=1 if jg==0: print("查找失败请重新输入") continue elif jg==1: break def dell_book(): book_name =input("请输入图书书名") i=0 j=0 jg=0 flag=[] while i<len(books): if books[i]["book_name"]==book_name: id=books[i]["book_id"] flag.append(id) print("图书编号:{}".format(id)) jg=1 i+=1 if jg==0: print("找不到图书名") if jg==1: book_id=input("请输入删除的图书编号") i=0 while j < len(flag): if flag[j] == book_id: while i < len(books): if books[i]["book_id"] == book_id: del books[i] new_file(books) print("删除成功") break i+=1 break j+=1 else: print("输入的编号有误") return books def modify_book(): book_name = input("请输入修改的图书名称") i = 0 jg = 0 flag = [] while i < len(books): if books[i]["book_name"] == book_name: id = books[i]["book_id"] flag.append(id) print("本书图书编号:{}".format(id)) jg = 1 i += 1 if jg == 0: print("找不到图书名") elif jg == 1: book_id = input("请输入修改的图书编号,或者其他需要修改的图书编号") for i in books: if book_id == i["book_id"]: New_id = input("新的图书编号") if New_id == i["book_id"]: print("输入编号重复") break else: New_name = input("新的图书名称") New_loc = input("新的图书位置") i["book_id"] = New_id i["book_name"] = New_name i["book_loc"] = New_loc new_file(books) break else: print("输入有误") def all_book(): for i in books: print(i) def read_book(data):#将文本文件里的图书信息读取到books中 f = open(data, mode="r", encoding="utf8") books = f.readlines() f.close() for i in range(0, len(books)): books[i] = eval(books[i].replace("\n", "")) return books def new_file(books):#将books的内容更新到文本文件里 i=0 nf = open('data', 'w+',encoding='utf8') # 以清空原文本文件内容的方式打开文件写入 while i<len(books): new=str(books[i]) nf.write(new) if i !=len(books)-1: nf.write("\n") i+=1 nf.close() if __name__=="__main__":#主要流程 while True: data="data" books=read_book(data) print_muen() choice=input("请输入您的选择") if choice=='1': add_book() all_book() elif choice=='2': find_book() elif choice=='3': dell_book() all_book() elif choice=='4': modify_book() all_book() elif choice == '5': all_book() elif choice == '6': print("感谢您的使用") sys.exit(0) else: print("您的输入有误,请重新输入")
还可以添加一个登录与注册系统:点击此处
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。