您需要创建一个Product class来完成所需操作。
然后创建这个类的一些Product实例。
使用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')
输出:
^{pr2}$