在python中使用类的项目价格

在python中使用类的项目价格,python,class,Python,Class,编写一个名为Product的类。该类应具有名为name、amount和price的字段, 持有该产品的名称、该产品的库存商品数量和常规价格 产品的价格。应该有一个方法get_price来接收 要购买的项目并返回购买该多个项目的成本,如果对少于10个项目的订单收取常规价格,则对介于之间的订单应用10%的折扣 10件和99件,订购100件或以上可享受20%的折扣。应该有 也可以是一个名为make_purchase的方法,该方法接收要购买的物品的数量和数量 减少了那么多 我的第一种方法很好用。然而,当

编写一个名为Product的类。该类应具有名为name、amount和price的字段, 持有该产品的名称、该产品的库存商品数量和常规价格 产品的价格。应该有一个方法get_price来接收 要购买的项目并返回购买该多个项目的成本,如果对少于10个项目的订单收取常规价格,则对介于之间的订单应用10%的折扣 10件和99件,订购100件或以上可享受20%的折扣。应该有 也可以是一个名为make_purchase的方法,该方法接收要购买的物品的数量和数量 减少了那么多

我的第一种方法很好用。然而,当我有了第二种方法时,它并没有做需要做的事情

class Product:

  def __init__(self,amount,price):
    self.amount=amount
    self.price=price

  def get_price(self):
    while self.amount>=0:
      if self.amount<10:
        return self.amount*self.price
      elif 10<=self.amount<99:
        return 0.9*self.amount*self.price
      else:
        return 0.8*self.amount*self.price

  def make_purchase(self):
    return int(input('Number of elements to buy'))-self.amount

costs=Product(int(input('Digit amount of items\n')),int(input('Digit price of items\n')))
purchases=Product(int(input('Digit amount of items to buy\n')),int(input('Digit price of items\n')))
print('Final costs = ',costs.get_price())
print('Elements to buy=',purchases.make_purchase())
您需要创建一个执行所需操作的产品类。 然后创建此类的一些产品实例。 您可以使用这些Product实例来调用类Product的方法,并获得所需的行为

每次输入所有的值都很尴尬,所以我编写了一些不需要输入的测试

当要购买的物品数量超过该物品的可用库存时,您可能需要决定要做什么;没有说明如何处理这起案件

class Product:

    def __init__(self, name, amount, price):
        self.name = name
        self.amount = amount
        self.price = price

    def get_price(self, number_to_be_bought):
        discount = 0
        if number_to_be_bought < 10:
            pass
        elif 10 <= number_to_be_bought < 99:
            discount = 10
        else:
            discount = 20
        price = (100 - discount) / 100 * self.price
        return price * number_to_be_bought

    def make_purchase(self, quantity):
        self.amount -= quantity


# name = input('name:')
# amount = int(input('Digit amount of items'))
# price = int(input('Digit price of items'))

name, amount, price = 'shoes', 200, 33

shoes = Product(name, amount, price)
# quantity = int(input('Digit amount of items to buy'))

q1 = 4
print(f'cost for {q1} {shoes.name} = {shoes.get_price(q1)}')
shoes.make_purchase(q1)
print(f'remaining stock: {shoes.amount}\n')

q2 = 12
print(f'cost for {q2} {shoes.name} = {shoes.get_price(q2)}')
shoes.make_purchase(q2)
print(f'remaining stock: {shoes.amount}\n')

q3 = 112
print(f'cost for {q3} {shoes.name} = {shoes.get_price(q3)}')
shoes.make_purchase(q3)
print(f'remaining stock: {shoes.amount}\n')

确切地说,它是如何做不到所要求的事情的?运行第二个方法会产生什么错误消息?第二个函数不会返回要购买的商品数量减去对象self.amountWelcome to StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确指定问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您指定的问题。您发布的代码需要手动输入,并且您不知道输出有什么问题。
cost for 4 shoes = 132.0
remaining stock: 196

cost for 12 shoes = 356.4
remaining stock: 184

cost for 112 shoes = 2956.8
remaining stock: 72