Python第二次作业

"""
题目描述
设计一个简单的图书借阅管理系统。系统初始包含若干本图书,每本图书的信息包括:

书号(字符串)

书名(字符串)

作者(字符串)

库存数量(整数)

另外,系统需记录借阅记录(列表,存储每笔借阅的字典)。程序通过菜单实现以下功能:

查看所有图书:以表格形式显示所有图书的书号、书名、作者、库存。

借书:输入书号和借阅数量,如果库存足够则减少库存,并添加一条借阅记录(包含书号、书名、借阅数量、借阅日期(可简化为当天日期字符串))。如果库存不足,提示失败。

还书:输入书号和归还数量,增加对应图书的库存,并从借阅记录中删除相应的借阅(按先进先出原则,或简单按书号匹配)。若归还数量大于该书的借出总量,则提示错误。

查询某本书的借阅情况:输入书号,显示该书当前被借出的总数量和所有借阅记录的详情(借阅数量、借阅日期)。

退出系统:结束程序。

要求:

数据初始化:至少预置 3 本图书和若干条借阅记录(可手动在代码中定义)。

菜单循环,用户输入 1~5 选择功能。

对用户输入做简单的有效性检查(如书号不存在、数量非法等)。

参考代码
python

初始化图书库存

books = {
"B001": {"name": "Python编程", "author": "张三", "stock": 5},
"B002": {"name": "数据结构", "author": "李四", "stock": 3},
"B003": {"name": "算法导论", "author": "王五", "stock": 0}
}

初始化借阅记录

borrow_records = [
{"book_id": "B001", "book_name": "Python编程", "quantity": 1, "date": "2026-03-25"},
{"book_id": "B002", "book_name": "数据结构", "quantity": 2, "date": "2026-03-26"}
]
"""

books = {
"B001": {"name": "Python编程", "author": "张三", "kucun": 5},
"B002": {"name": "数据结构", "author": "李四", "kucun": 3},
"B003": {"name": "算法导论", "author": "王五", "kucun": 2}
}

borrow_records = [
{"book_id": "B001", "book_name": "Python编程", "quantity": 1, "date": "2026-03-25"},
{"book_id": "B002", "book_name": "数据结构", "quantity": 2, "date": "2026-03-26"}
]

while True:
str1 = ("\n=====图书借阅管理系统====="
"\n查看所有图书"
"\n借书"
"\n还书"
"\n查询借阅情况"
"\n退出")
print(str1)

choose = input("请选择1-5:")

if choose == "1":
    print("=====所有图书=====")
    print("书号\t\t\t书名\t\t作者\t\t库存\t")
    for shuhao in books:
        print(shuhao, "\t", books[shuhao]["name"], "\t", books[shuhao]["author"], "\t", books[shuhao]["kucun"])
    print()

elif choose == "2":
    shuhao = input("请输入书号:")
    if shuhao not in books:
        print("书号不存在\n")
    else:
        num = int(input("请输入借阅数量:"))
        if num <= 0:
            print("数量必须大于0\n")
        else:
            if books[shuhao]["kucun"] < num:
                print("库存不足\n")
            else:
                books[shuhao]["kucun"] -= num
                record = {
                    "book_id": shuhao,
                    "book_name": books[shuhao]["name"],
                    "quantity": num,
                    "date": "2026-04-07"
                }
                borrow_records.append(record)
                print("借书成功\n")

elif choose == "3":
    shuhao = input("请输入书号:")
    if shuhao not in books:
        print("书号不存在\n")
    else:
        num = int(input("请输入归还数量:"))
        if num <= 0:
            print("数量必须大于0\n")
        else:
            total = 0
            for cishu in borrow_records:
                if cishu["book_id"] == shuhao:
                    total += cishu["quantity"]
            if num > total:
                print("归还数量超过借出总量\n")
            else:
                books[shuhao]["kucun"] += num
                need = num
                new_records = []
                for cishu in borrow_records:
                    if cishu["book_id"] == shuhao and need > 0:
                        if cishu["quantity"] <= need:
                            need -= cishu["quantity"]
                        else:
                            cishu["quantity"] -= need
                            need = 0
                            new_records.append(cishu)
                    else:
                        new_records.append(cishu)
                borrow_records = new_records
                print("还书成功\n")

elif choose == "4":
    shuhao = input("请输入书号:")
    if shuhao not in books:
        print("书号不存在\n")
    else:
        total = 0
        records = []
        for cishu in borrow_records:
            if cishu["book_id"] == shuhao:
                total += cishu["quantity"]
                records.append(cishu)
        print("\n借出总数:", total)
        print("借阅记录:")
        for cishu in records:
            print(cishu["quantity"], "本", cishu["date"])
        print()

elif choose == "5":
    print("退出系统")
    break

else:
    print("输入错误,请重新选择\n")
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 题目描述设计一个简单的图书借阅管理系统。系统初始包含若干本图书,每本图书的信息包括: 书号(字符串) 书名(字符串...
    千古风流阅读 36评论 0 1
  • 1. 使用列表推导式,求出所有满足“周长为24且每条边长均为整数”要求的三角形的边长,且相同边长的三角形不重复出现...
    吃茶的武士阅读 638评论 0 0
  • 感觉终于有时间来补作业了,漫长的六天工作日迎来了短暂的休息。闲话不多说,直接写心得。没有时间敲代码的时候就在本子上...
    安sir君阅读 619评论 2 3
  • Python制作进度条安装 tqdm库pip install 库的名称导入 tqdm 字符串表示 '' " "要注...
    说书客_8a07阅读 400评论 0 0

友情链接更多精彩内容