
简易Python代码---购物清单
发布日期:2021-05-14 16:02:32
浏览次数:24
分类:精选文章
本文共 2252 字,大约阅读时间需要 7 分钟。
购物清单Python代码:
# 定义仓库repository = dict()# 定义购物清单shop_list = []# 初始化商品def init_repostory(): goods1 = ("001", "Python", 66) goods2 = ("002", "C++", 88) goods3 = ("003", "Linux", 90) goods4 = ("004", "MySql", 35) # 商品入库 repository[goods1[0]] = goods1 repository[goods2[0]] = goods2 repository[goods3[0]] = goods3 repository[goods4[0]] = goods4# 商品清单def show_goods(): print("欢迎光临,欣悦超市") print("商品清单:") print('%13s%40s%10s' % ("条码", "商品名", "单价")) # 遍历 for goods in repository.values(): print('%15s%40s%12s' % goods)# 显示购物清单def show_list(): print("=" * 100) # 输出 if not shop_list: print("未购物") else: title = '%-5s|%15s|%40s|%10s|%4s|%10s' % \ ("ID", "条码", "商品名", "单价", "数量", "小计") print(title) print("-" * 100) # 总计 sum = 0 # 遍历清单 for i, item in enumerate(shop_list): id = i + 1 code = item[0] name = repository[code][1] price = repository[code][2] number = item[1] amount = price * number sum = sum + amount line = '%-5s|%17s|%40s|%12s|%6s|%12s' % \ (id, code, name, price, number, amount) print(line) print("-" * 100) print(" 总计:", sum) print("=" * 100)# 添加购买商品def add(): code = input("输入条码:\n") if code not in repository: print("条码错误") return # 找商品 goods = repository[code] # 数量 number = input("购买数量:\n") shop_list.append([code, int(number)])# 修改数量def edit(): id = input("修改的ID: \n") index = int(id) -1 item = shop_list[index] # 新的数量 number = input("新的购买数量:\n") item[1] = int(number)# 删除def delete(): id = input("删除的ID:") index = int(id) - 1 del shop_list[index]def payment(): show_list() print('\n' * 3) print("下次光临") # 退出 import os os._exit(0)cmd_dict = {'a': add, 'e': edit, 'd': delete, 'p': payment, 's': show_goods}#命令提示def show_command(): cmd = input("输入操作指令:\n" + " 添加(a) 修改(e) 删除(d) 结算(p) 超市商品(s)\n") if cmd not in cmd_dict: print("按指示输入") else: cmd_dict[cmd]()init_repostory()show_goods()while True: show_list() show_command()
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月28日 07时47分17秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
计算机网络UDP协议和TCP协议
2021-05-14
Linux运行C语言文件
2021-05-14
C字符串高级
2021-05-14
2010-03-25 函数题
2021-05-14
C语言_动态内存分配练习
2021-05-14
Linux学习_系统进程概念
2021-05-14
七层网络模型(待添加)
2021-05-14
考研复试——KY276 Problem C
2021-05-14
LeetCode62/63/64 不同路径I/II/最小路径和
2021-05-14
LeetCode 45/55. 跳跃游戏I/II
2021-05-14
老鸟带你画tiled lines
2021-05-14
MybatisPlus自定义Sql实现多表查询
2021-05-15
Java位运算,负数的二进制表示形式,int类型最大值为什么是2的31次方-1
2021-05-15
WIFI模块开发教程之W600网络篇3:STA模式下TCP Client通信
2021-05-15
PyQt5快速上手基础篇10-QSettings用法
2021-05-15
JQuery--手风琴,留言板
2021-05-15
vue--搜索,添加,删除小案例
2021-05-15
VUE框架应用包---------微信二维码应用
2021-05-15
(KOA2 step1)利用koa-generator创建KOA2项目
2021-05-15
MFC 自定义消息发送字符串
2021-05-15