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

Python购物车程序参数与方法调用问题咨询

class store:
    tax = 0.1

    def __init__(self, name: str, price: float, quantity=0):
        assert(price >= 0), f"Price {price} is invalid"
        assert (quantity >= 0), f"Quantity {quantity} is invalid"

        self.mainQ = None
        self.name = name
        self.price = price
        self.quantity = quantity

    # def calculate_total_price(self):
    #     print(self.price * self.quantity * store.tax + self.price * self.quantity)
    #     return self.price * self.quantity * store.tax + (self.price * self.quantity)

    # def Update(self, mainQ):
    #     self.mainQ = self.quantity

# Index is the index of the quantity you want to buy
    def purchase(self, index):
        a = self.price * self.quantity * store.tax + self.price * self.quantity
        itemData["quantity"][index] = self.mainQ - self.quantity

        return a, itemData

itemData = {
    "Item": ["Book", "Laptop", "Phone"],
    "Quantity": [10, 15, 5],
    "Price": [2.5, 100, 50]
}

b = store.purchase(1)

问题

  • 调用b = store.purchase(1)时,编辑器提示“parameter 'index' unfilled”,是否需将index加入__init__方法才能访问?
  • 程序是否存在缺失参数导致无法正常调用?
  • 我希望根据用户选择的购买数量更新库存,当前的数量更新逻辑是否正确?
    补充:尝试通过b = store.purchase(itemData["Item"][0], itemData["Price"][0], itemData["Quantity"][0])调用方法时,添加Quantity参数后提示“unexpected argument”。

解决方案

关于调用错误的问题(问题1、2及补充)

你犯了两个核心错误:

  1. purchase是实例方法,必须先创建store类的实例,再通过实例调用,不能直接用类名调用。直接用类调用时,第一个参数会被当成self,原本的index参数就没传,所以会提示“parameter 'index' unfilled”。
  2. purchase方法只定义了index一个参数,你后面调用时传了三个参数,自然会报“unexpected argument”。

正确的调用步骤:

# 1. 先创建store实例,传入商品名称、单价、购买数量
book_store = store(
    name=itemData["Item"][0],
    price=itemData["Price"][0],
    quantity=2  # 假设购买2本
)
# 2. 通过实例调用purchase,传入该商品在itemData中的索引
b = book_store.purchase(0)

不需要把index加入__init__方法,它是purchase的专属参数,调用时传入即可。

关于库存更新逻辑的问题(问题3)

当前逻辑完全错误,存在三个致命问题:

  • self.mainQ初始值是None,直接做减法运算会触发TypeError;
  • itemData的键是"Quantity"(首字母大写),代码里写的是itemData["quantity"](小写),会触发KeyError;
  • 没有正确关联原始库存和购买数量的逻辑,无法实现库存扣减。

修正后的逻辑:

class store:
    tax = 0.1

    def __init__(self, name: str, price: float, buy_quantity=0, stock=0):
        assert(price >= 0), f"Price {price} is invalid"
        assert (buy_quantity >= 0), f"Buy quantity {buy_quantity} is invalid"
        assert (stock >= 0), f"Stock {stock} is invalid"

        self.stock = stock  # 传入当前库存
        self.name = name
        self.price = price
        self.buy_quantity = buy_quantity  # 购买数量

    def purchase(self, index):
        # 检查库存是否充足
        assert self.stock >= self.buy_quantity, "库存不足,无法完成购买"
        # 计算含税总价
        total_price = self.price * self.buy_quantity * (1 + store.tax)
        # 更新库存(注意键名是大写的Quantity)
        itemData["Quantity"][index] = self.stock - self.buy_quantity
        return total_price, itemData

# 创建实例时传入对应商品的当前库存
book_stock = itemData["Quantity"][0]
book_store = store(
    name=itemData["Item"][0],
    price=itemData["Price"][0],
    buy_quantity=2,
    stock=book_stock
)
# 调用purchase完成购买和库存更新
result = book_store.purchase(0)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 03:01:28