python项目报价_使用python类的项目价格

您需要创建一个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}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值