(资料图片)
# 一个商品 名称 价格 库存 总销量# 存放商品的数据类型 str set list tuple dict# 综合考虑,选择字典dict_data = {}# {名称: {price: 价格, inventory: 库存, sales: 总销量}}def exist_goods(goods_name): # 有无keys都行应该是 """判断商品是否已经存在""" if goods_name in dict_data: return True else: return False# 补货,添加商品数据def add_goods(): """补货""" goods_name = input("商品的名称:") goods_count = int(input("商品的数量:")) # 该商品是否已经存在 if exist_goods(goods_name): # 有无keys都行应该是 # 如果该商品已经存在 # 就添加对应的数量 dict_data[goods_name]["inventory"] += goods_count # 添加对应的库存 else: goods_price = float(input("商品的价格:")) # 把商品数据添加到字典 dict_data[goods_name] = {"price": goods_price, "inventory": goods_count, "sales": 0} print(f"{goods_name}补货成功,当前库存为{dict_data[goods_name]["inventory"]}")# 出售商品def sell_goods(): """出售商品,库存减少,销量增加""" goods_name = input("商品的名称:") if exist_goods(goods_name): goods_count = int(input("商品的数量:")) dict_data[goods_name]["inventory"] -= goods_count # 添加对应的库存 dict_data[goods_name]["sales"] += goods_count # 统计总销量 print("出售成功") else: print("商品名称不存在")# 修改商品的价格def modify_price(): """修改商品的价格""" goods_name = input("商品的名称:") if exist_goods(goods_name): good_price = float(input("商品的价格:")) dict_data[goods_name]["price"] = good_price # 修改价格 print("修改成功") else: print("商品名称不存在")# 查询单个商品的信息def select_goods(): """查询单个商品的信息""" goods_name = input("商品的名称:") if exist_goods(goods_name): print(dict_data[goods_name]) else: print("商品不存在")# 查询所有商品的信息def select_all_goods(): """查询所有商品的信息""" for i in dict_data.items(): print(i)while True: print("""----------------------------------------------------------------------------1.增加商品 2.出售商品 3.修改商品价格 4.查询单个商品数据 5.查询所有商品数局 6.退出""") q = input("输入要操作的对象的id:") if q == "1": add_goods() elif q == "2": sell_goods() elif q == "3": modify_price() elif q == "4": select_goods() elif q == "5": select_all_goods() elif q == "6": break else: print("操作id无效")