函数式编程实战


使用函数式编程将图书管理系统进行改造

BOOKS = []


def main():
    while True:
        print('-' * 50)
        print('欢迎使用图书管理系统!')
        print('-' * 50)
        print('增加图书【1】删除图书【2】修改图书【3】查看图书【4】')
        print('-' * 50)
        num = input('请输入数字指令:')
        if num == '1':
            add()
        elif num == '2':
            delete()
        elif num == '3':
            modify()
        elif num == '4':
            search()
        else:
            print('您的输入错误!')


def add():
    book = input('请您输入书名:')
    BOOKS.append(book)
    print(f'{book}增加成功!')


def delete():
    book = input('请您输入需要删除的书名:')
    if book in BOOKS:
        BOOKS.remove(book)
        print(f'{book}删除成功!')
    else:
        print('您输入的书名不存在!')


def modify():
    book = input('请您输入需要修改的书名:')
    if book in BOOKS:
        newbook = input('请您输入新的书名:')
        BOOKS[BOOKS.index(book)] = newbook
        print(f'{book}已修改为{newbook}!')
    else:
        print('您输入的书名不存在!')


def search():
    if BOOKS == []:
        print('目前没有图书!')
    else:
        for book in BOOKS:
            print(f'书名:{book}')


main()

results matching ""

    No results matching ""