发布网友 发布时间:2022-03-03 15:56
共1个回答
热心网友 时间:2022-03-03 17:26
from random import randint
class Shengfen(object):
def __init__(self, sname):
self.sname = sname
def shengfen(self):
print(self.sname)
class Dijishi(Shengfen):
def __init__(self, sname, dname):
Shengfen.__init__(self, sname)
self.dname = dname
self.money = 10000 #现有钱数
self.number = 0 #商品数量
self.jycs = 0 #交易次数
def attach_g(self): #联系供应商
self.money -= 100
self.number += 1
if self.number >= 4:
self.xiaoshou() #库存积攒到4件时,开始销售该商品
print("现有钱数:" + str(self.money))
print("现有商品数量:" + str(self.number))
def xiaoshou(self): #销售该商品
if self.jycs < 4: #交易次数小于4次,继续交易
self.money += 110
self.number -= 1
self.jycs += 1
else: #交易次数大于4次,又百分之50的几率退货
if randint(1,2) == 1: #用randint函数创建1/2概率
self.money += 110
self.number -= 1
else:
self.tuihuo()
def tuihuo(self):
self.money -= 30 #退货一件,相当于亏了30元
print("退货一件")
class Gebishi(Shengfen):
def __init__(self, sname, gname):
Shengfen.__init__(self, sname)
self.gname = gname
if __name__ == "__main__":
count = 9 #交易次数
Qinshihuang = Dijishi('Guangdong', 'Guangzhou')
Liubang = Gebishi('Guangdong', 'Foshan')
Hanxin = Gebishi('Guangdong', 'Foshan')
for i in range(count):
Qinshihuang.attach_g()
大致按照要求来写了一下,可能有些地方还有问题,如果有的话,你留言一下,我再帮你改吧
追问你好,请问这个是什么原因呢