News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

python实现图书管理系统(超详细)

posted on 2023-05-21 18:36     read(339)     comment(0)     like(20)     collect(2)


Python implements library management system


Experimental environment: PyCharm 2021.1.3 x64 Professional Edition
insert image description here

Library management system:

Data storage system functions
book data:

Book id (each id corresponds to a book)
book name
book location
whether the book is lent

The next step is data storage. Data storage has links: lists , tuples , dictionaries , etc. In this book management system, we use dictionaries for storage. In general, it is recommended to use dictionaries for complex data storage. After learning the file operation later , you can store the data in the file for easy use. In this experiment, basically every line of code has a corresponding comment, which is convenient for readers to read. If you encounter any problems that you don’t understand, you can private message me, and I will help you solve them! !

In this experiment, I will separate the modules and explain them one by one for the convenience of readers.

The storage form of book information in the dictionary:

The book id is used as the key
, the book name, the location of the book, and whether the book is loaned out as the value

There are also disadvantages to storing data in dictionaries. Data cannot be stored persistently and can only be used temporarily in memory.

Book features:

Book functions are generally four functions of adding, deleting, modifying and checking.
Add: add books
, delete: delete books
, change: modify book location (book id and book name cannot be modified)
check: book query (book information query)
book information query includes:
book id query books Name query, book location query,
and another function, the lending and returning function of books

insert image description here
insert image description here

Now start adding data:
books_dict = {}
数据先存储到字典中

Store those data including:
book id book name book location

increase data

Step 1:
Create the function of adding books.
The code is as follows:

def add_book():
    """增加图书信息"""
    book_id = input("请输入书的id:")  # key
    book_name = input("请输入书的名称:")  # value
    book_position = input("请输入书的位置:")  # value
    is_lend = False  # 是否借出

    books_dict[book_id] = {"book_name": book_name,
                           "book_position": book_position,
                           "is_lend": is_lend}  # 将输入的图书信息存储到字典
    print(f"数据添加完成:{books_dict}:{books_dict[book_id]}")  # 显示添加数据后的信息

delete data

To delete data, delete by querying the id.
The code is as follows:


def del_book():
    """删除图书信息"""
    book_id = input("请输入书的id:")
    book_info = books_dict[book_id]   # 通过key获取字典的数据 字典数据的获取
    del books_dict[book_id]           # 删除字典里面指定key数据  字典
    print(f"删除图书:{book_id}:{book_info}")

Query data

Find book information
including: find detailed books, find books that have been borrowed, and view all book information.
The code is as follows:

def select_book(): # 查找详细的图书 查找已经借出的图书 查看所有的图书信息
    """查找图书的信息"""
    print("1:查找详细的图书 2:查找已经借出的图书 3:查看所有的图书信息")
    sub_code = input("请您输入需要使用的功能:")

    if sub_code == "1":
        book_id = input("请输入书的id:")
        print(books_dict[book_id])      # 通过key获取字典key相关的数据

    elif sub_code == "2":
        for i in books_dict.items():   # 字典课程 字典操作方法
            if i[1]["is_lend"]:        # 筛选字典的数据 i[1] 字符串的操作 字符串的索引
                print(i)

    elif sub_code == "3":
        for i in books_dict.items():
            print(i)

Book location modification

As mentioned earlier, the book id and book name are unique and cannot be modified, so the only thing we can modify is the location of the book.
code show as below:

def modify_book():
    """图书位置的修改"""    # 字典数据的修改
    book_id = input("请输入书的id:")  # key
    book_position = input("请输入书存放新的位置:")
    books_dict[book_id]['book_position'] = book_position
    print(f"修改后的数据:{book_id}:{books_dict[book_id]}")


lending of books

Lend books by id (If you use the book name, an error will occur, because there may be more than one book with the same name.)

def lend_book():
    """图书的借出"""
    book_id = input("请输入书的id:\n")
    books_dict[book_id]["is_lend"] = True

return of books

Return by book id

def give_back():
    """图书的还回"""
    book_id = input("请输入书的id:\n")
    books_dict[book_id]["is_lend"] = False

main interface


while True:  # while 循环
    print("-" * 60)     # * 复制容器里面的数据
    print("1:图书添加  2:图书删除 3:图书位置修改 \n "
          "4:图书借出  5:图书还回 6:图书信息查看  7:退出系统")
    func_code = input("请您输入需要是使用的功能:")
    print("-" * 60)

    # func_dict = {"1": add_book}
    # 这么写可以让我们以更简单的方式来访问字典中的函数。而不是用字典中的字符串指定函数,我们可以直接用字典中的整数访问函数。

    if func_code == "1":  # func_code 需要注意输入的数据类型
        add_book()        # 函数的调用
    elif func_code == "2":
        del_book()
    elif func_code == "3":
        modify_book()
    elif func_code == "4":
        lend_book()
    elif func_code == "5":
        give_back()
    elif func_code == "6":
        select_book()
    elif func_code == '7':
        break        # 循环里面的关键字  只能在循环里面使用
    else:
        print("输入的选项id无效!")
';

full code


books_dict = {}

def add_book():
    """增加图书信息"""
    book_id = input("请输入书的id:")  # key
    book_name = input("请输入书的名称:")  # value
    book_position = input("请输入书的位置:")  # value
    is_lend = False  # 是否借出

    books_dict[book_id] = {"book_name": book_name,
                           "book_position": book_position,
                           "is_lend": is_lend}  # 将输入的图书信息存储到字典
    print(f"数据添加完成:{books_dict}:{books_dict[book_id]}")  # 显示添加数据后的信息


def del_book():
    """删除图书信息"""
    book_id = input("请输入书的id:")
    book_info = books_dict[book_id]   # 通过key获取字典的数据 字典数据的获取
    del books_dict[book_id]           # 删除字典里面指定key数据  字典
    print(f"删除图书:{book_id}:{book_info}")


def select_book(): # 查找详细的图书 查找已经借出的图书 查看所有的图书信息
    """查找图书的信息"""
    print("1:查找详细的图书 2:查找已经借出的图书 3:查看所有的图书信息")
    sub_code = input("请您输入需要使用的功能:")

    if sub_code == "1":
        book_id = input("请输入书的id:")
        print(books_dict[book_id])      # 通过key获取字典key相关的数据

    elif sub_code == "2":
        for i in books_dict.items():   # 字典课程 字典操作方法
            if i[1]["is_lend"]:        # 筛选字典的数据 i[1] 字符串的操作 字符串的索引
                print(i)

    elif sub_code == "3":
        for i in books_dict.items():
            print(i)


def modify_book():
    """图书位置的修改"""    # 字典数据的修改
    book_id = input("请输入书的id:")  # key
    book_position = input("请输入书存放新的位置:")
    books_dict[book_id]['book_position'] = book_position
    print(f"修改后的数据:{book_id}:{books_dict[book_id]}")


def give_back():
    """图书的还回"""
    book_id = input("请输入书的id:\n")
    books_dict[book_id]["is_lend"] = False


def lend_book():
    """图书的借出"""
    book_id = input("请输入书的id:\n")
    books_dict[book_id]["is_lend"] = True


while True:  # while 循环
    print("-" * 60)     # * 复制容器里面的数据
    print("1:图书添加  2:图书删除 3:图书位置修改 \n "
          "4:图书借出  5:图书还回 6:图书信息查看  7:退出系统")
    func_code = input("请您输入需要是使用的功能:")
    print("-" * 60)

    # func_dict = {"1": add_book}
    # 这么写可以让我们以更简单的方式来访问字典中的函数。而不是用字典中的字符串指定函数,我们可以直接用字典中的整数访问函数。

    if func_code == "1":  # func_code 需要注意输入的数据类型
        add_book()        # 函数的调用
    elif func_code == "2":
        del_book()
    elif func_code == "3":
        modify_book()
    elif func_code == "4":
        lend_book()
    elif func_code == "5":
        give_back()
    elif func_code == "6":
        select_book()
    elif func_code == '7':
        break        # 循环里面的关键字  只能在循环里面使用
    else:
        print("输入的选项id无效!")

Function running screenshot

Book add:
insert image description here
insert image description here

Book deletion:
insert image description here
Book location modification:
insert image description here
Book lending:
insert image description here
Book return:
insert image description here
Book information viewing:
insert image description here
Exit the system:insert image description here

insert image description here

"For the past 33 years, I have looked in the mirror every morning and asked myself: 'Is this the last day of my life? Am I going to do what I have to do today? "It's "no" too many times a day, and I know it needs to change...all things—all external expectations, all pride, all fear of difficulty and failure—those things disappear in the face of death No trace, only what's really important. Thinking I'm going to die is the best way to keep me from falling into the trap of worrying about losing something."
- Steve Jobs



Category of website: technical article > Blog

Author:Soledad

link:http://www.pythonblackhole.com/blog/article/25342/4c81eb24fea0b04ec731/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

20 0
collect article
collected

Comment content: (supports up to 255 characters)