You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python OOP嵌套字典存储商品无法累加库存计数问题

问题:在线购物系统重复上传商品时库存累加逻辑失效

当前通过实操项目练习面向对象编程,开发的在线购物系统需支持管理员上传商品、用户购买商品功能,预期重复上传同一商品时不新增条目,仅累加对应商品库存,但现有代码无法实现该效果。

现有代码

Product 商品类定义

class Product:
  def __init__(self, product_name, description, price, company, product_count):
   self.product_name = product_name
   self.description = description
   self.price = price
   self.company = company
   self.product_count = product_count

商品对象实例化示例

#CREATE PRODUCT
laptop = Product('Dell Inspiron', 'Dell Inspiron with 8GB ram', 1500, 'DELL', 100)
tv = Product('Sony TV', 'Best Sony TV Ever', 2000, 'Sony', 100)
smartwatch = Product('Fossil 6 Gen Smartwatch', 'Fossil watch with wear OS', 300, 'Fossil',100)
shoes = Product('Running shoes', 'Medium size running shoes', 100, 'Nike',100)
gloves = Product('Rock CLimbing gloves', 'Medium gloves for rock clmbing', 250, 'Timberland',100)

Admin 管理员类定义

class Admin:
  def __init__(self, name):
    self.name = name
    self.products = {}
    self.order = {}
    self.total_products = 0
    self.orders_recieved = {}
    self.order_status = 'Order Recieved'

现有add_products商品添加方法

def add_products(self, product):
    if product not in self.products:
      self.products[product.product_name] = {
        'name':product.product_name,
        'description':product.description,
        'price': product.price,
        'company':product.company,
        'count':product.product_count
      }
      if product not in s.site_products:  #I am also storing all products in different dict() ignore this
        s.site_products[product.product_name] = {
          'name':product.product_name,
          'description':product.description,
          'price': product.price,
          'company':product.company,
          'count':product.product_count
        }
      s.product_total += 1
      self.total_products += 1
    else:
      product.product_count += product.product_count
      
      print('Product already exists')

方法调用示例

admin1.add_products(laptop)
admin1.add_products(laptop)

admin1.add_products(tv)
admin1.add_products(smartwatch)

admin2.add_products(shoes)
admin2.add_products(gloves)

根因分析

代码存在3个核心逻辑错误:

  1. 存在性判断规则不匹配:self.products字典的键是字符串类型的商品名(product.product_name),但判断条件写的是if product not in self.products,即检查传入的Product类实例是否在字典的键中,对象和字符串永远不可能相等,该条件永远为True,每次调用都会走新增商品分支,永远进入不了else的累加逻辑。
  2. 累加对象错误:即使进入else分支,现有代码product.product_count += product.product_count只是把传入的商品实例自身的数量翻倍,完全没有修改self.products和s.site_products字典中已经存储的商品库存值,库存数据不会有任何更新。
  3. 计数逻辑错误:每次进入新增分支都会执行s.product_total += 1,重复上传商品时会错误统计全站商品总数。

修复方案

调整add_products方法的判断逻辑和累加逻辑,修正后代码如下:

def add_products(self, product):
    # 统一用商品名作为商品是否存在的判断依据,和字典存储键规则对齐
    if product.product_name not in self.products:
      self.products[product.product_name] = {
        'name':product.product_name,
        'description':product.description,
        'price': product.price,
        'company':product.company,
        'count':product.product_count
      }
      # 同步修正全局商品字典的判断逻辑
      if product.product_name not in s.site_products:
        s.site_products[product.product_name] = {
          'name':product.product_name,
          'description':product.description,
          'price': product.price,
          'company':product.company,
          'count':product.product_count
        }
        # 仅全站首次新增商品时,才累加全站商品总数
        s.product_total += 1
      # 仅管理员首次新增该商品时,累加管理员名下商品总数
      self.total_products += 1
    else:
      # 同步更新两个存储字典中的库存值
      self.products[product.product_name]['count'] += product.product_count
      s.site_products[product.product_name]['count'] += product.product_count
      print('Product already exists, stock updated successfully')

逻辑说明

  • 首次上传某商品时,正常创建商品条目,计数累加规则保持不变
  • 重复上传同一商品时,自动将新传入的商品数量累加到已有库存上,不会生成重复条目,也不会错误统计商品总数
  • 管理员私有商品字典和全局站点商品字典的库存值会同步更新,避免数据不一致

内容的提问来源于stack exchange,提问作者Sumit Dadwal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 10:42:23