news 2026/5/15 22:12:40

python 第二次作业

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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"} ] while True: print("\n菜单:") print("1. 查看所有图书") print("2. 借书") print("3. 还书") print("4. 查询借阅情况") print("5. 退出") choice = input("请选择功能 (1-5): ") if choice == '1': print("\n所有图书:") print("书号\t\t\t\t书名\t\t\t\t作者\t\t\t\t\t库存") print("-" * 55) for book_id, info in books.items(): print(f"{book_id}\t\t{info['name']}\t\t\t{info['author']}\t\t\t\t\t{info['stock']}") print("-" * 55) elif choice == '2': book_id = input("请输入书号: ") if book_id not in books: print("书号不存在。") continue quantity_str = input("请输入借阅数量: ") if not quantity_str.isdigit(): print("借阅数量必须是正整数。") continue quantity = int(quantity_str) if quantity <= 0: print("借阅数量必须大于0。") continue if books[book_id]['stock'] < quantity: print("库存不足。") continue books[book_id]['stock'] -= quantity borrow_records.append({ "book_id": book_id, "book_name": books[book_id]['name'], "quantity": quantity, "date": "2026-04-05" }) print("借书成功。") elif choice == '3': book_id = input("请输入书号: ") if book_id not in books: print("书号不存在。") continue quantity_str = input("请输入归还数量: ") if not quantity_str.isdigit(): print("归还数量必须是正整数。") continue quantity = int(quantity_str) if quantity <= 0: print("归还数量必须大于0。") continue total_borrowed = sum(record['quantity'] for record in borrow_records if record['book_id'] == book_id) if quantity > total_borrowed: print("归还数量大于该书的借出总量。") continue books[book_id]['stock'] += quantity remaining = quantity i = 0 while i < len(borrow_records): if borrow_records[i]['book_id'] == book_id: if borrow_records[i]['quantity'] <= remaining: remaining -= borrow_records[i]['quantity'] borrow_records.pop(i) else: borrow_records[i]['quantity'] -= remaining break else: i += 1 print("还书成功。") elif choice == '4': book_id = input("请输入书号: ") if book_id not in books: print("书号不存在。") continue total_borrowed = sum(record['quantity'] for record in borrow_records if record['book_id'] == book_id) print(f"\n书号 {book_id} 被借出的总数量: {total_borrowed}") print("借阅记录:") for record in borrow_records: if record['book_id'] == book_id: print(f" 借阅数量: {record['quantity']}, 借阅日期: {record['date']}") elif choice == '5': print("退出系统。") break else: print("无效选择,请重新输入。")

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/15 22:11:48

在超大数据集下 DuckDB 与 MySQL 查询速度对比俗

一、什么是urllib3&#xff1f; urllib3 是一个用于处理 HTTP 请求和连接池的强大、用户友好的 Python 库。 它可以帮助你&#xff1a; 发送各种 HTTP 请求&#xff08;GET, POST, PUT, DELETE等&#xff09;。 管理连接池&#xff0c;提高网络请求效率。 处理重试和重定向。 支…

作者头像 李华
网站建设 2026/5/15 22:12:23

如何用XXMI启动器一站式管理六大热门二次元游戏模组?

如何用XXMI启动器一站式管理六大热门二次元游戏模组&#xff1f; 【免费下载链接】XXMI-Launcher Modding platform for GI, HSR, WW and ZZZ 项目地址: https://gitcode.com/gh_mirrors/xx/XXMI-Launcher 你是否厌倦了为每款游戏单独安装和管理模组&#xff1f;是否曾因…

作者头像 李华
网站建设 2026/5/15 22:12:08

OpenClaw云端体验方案:Qwen3-14B镜像一键部署实践

OpenClaw云端体验方案&#xff1a;Qwen3-14B镜像一键部署实践 1. 为什么选择云端体验OpenClaw 上周我在本地笔记本上折腾OpenClaw时&#xff0c;被Python版本冲突和CUDA依赖折磨得够呛。正当准备放弃时&#xff0c;偶然发现星图平台提供了Qwen3-14BOpenClaw的预置镜像组合。这…

作者头像 李华
网站建设 2026/5/1 5:07:55

3分钟学会:如何通过手机号码快速定位地理位置?

3分钟学会&#xff1a;如何通过手机号码快速定位地理位置&#xff1f; 【免费下载链接】location-to-phone-number This a project to search a location of a specified phone number, and locate the map to the phone number location. 项目地址: https://gitcode.com/gh_…

作者头像 李华
网站建设 2026/4/12 3:35:07

django基于数据挖掘的高考志愿推荐系统的设计与实现_3ri592gf_zl033

前言&emsp高考志愿填报是每位高考生必须面对的重要环节&#xff0c;它直接关系到考生的未来学习和职业发展。然而&#xff0c;由于高考志愿的复杂性和多样性&#xff0c;很多考生在填报志愿时感到困惑和迷茫。传统的志愿填报方式主要依赖学校老师的经验、招生简章和家长的经…

作者头像 李华
网站建设 2026/4/12 18:44:46

MTK设备救砖终极指南:使用MTKClient快速修复联发科设备

MTK设备救砖终极指南&#xff1a;使用MTKClient快速修复联发科设备 【免费下载链接】mtkclient MTK reverse engineering and flash tool 项目地址: https://gitcode.com/gh_mirrors/mt/mtkclient MTKClient是一款专业的联发科芯片底层刷机和修复工具&#xff0c;能够直…

作者头像 李华