简易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()

 

上一篇:简易通讯录管理系统(C++实现)
下一篇:SQL2008 重启计算机失败

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月28日 07时47分17秒