|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
class Car_General:
def __init__(self,carid,brand,model,platenum,dayrent):
self.brand=brand
self.model=model
self.platenum=platenum
self.dayrent=dayrent
self.carid=carid
def get_brand(self):
return self.brand
def get_model(self):
return self.model
def get_platenum(self):
return self.platenum
def get_dayrent(self):
return self.dayrent
def get_carid(self):
return self.carid
def calc_rent(self,day,adiscount=1):
self.day=day
self.adiscount=adiscount
self.rent=self.day*(self.dayrent-self.bonus+self.insurance+self.loss)*self.adiscount
class EconomyCar(Car_General):
def __init__(self,bonus):
super.__init__(carid,brand,model,platenum,dayrent)
self.bonus=bonus
class LuxuryCar(Car_General):
def __init__(self,insurance):
super.__init__(carid,brand,model,platenum,dayrent)
self.insurance=insurance
class SportsCar(Car_General):
def __init__(self,loss):
super.__init__(carid,brand,model,platenum,dayrent)
self.loss=loss
class SUV(Car_General):
def __init__(self,adiscount):
super.__init__(carid,brand,model,platenum,dayrent)
self.adiscount=adiscount
class CarOperation():
cars={}
stocks={'EconomyCar':[],'LuxuryCar':[],'SportsCar':[],'SUV':[]}
carid1=10000
carid2=20000
carid3=30000
carid4=40000
def operate(self):
print('欢迎使用鱼C汽车租赁程序\n')
while True:
op=int(input('1.录入汽车;2.租车服务;3.还车服务;4.退出程序:\n'))
if op == 1:
self.register()
if op == 2:
self.rent_car()
if op == 3:
self.return_car()
if op == 4:
break
def register(self):
sort_=int(input('1.经济型车;2.豪华车;3.跑车;4.SUV:'))
num=int(input('请输入需要录入的数量:'))
for i in range(num):
print(f'请录入第{i+1}辆车\n')
brand=input('品牌:')
model=input('型号:')
platenum=input('车牌:')
dayrent=int(input('租金:'))
if sort_ ==1:
bonus=int(input('补贴:'))
carid=self.carid1
self.carid1+=1
c=Car_General(carid,brand,model,platenum,dayrent,bonus)
if sort_ ==2:
insurance=int(input('保险:'))
carid=self.carid2
self.carid2+=1
c=Car_General(carid,brand,model,platenum,dayrent,insurance)
if sort_ ==3:
loss=int(input('损耗:'))
carid=self.carid3
self.carid3+=1
c=Car_General(carid,brand,model,platenum,dayrent,loss)
if sort_ ==4:
carid=self.carid4
self.carid4+=1
if day>7:
adiscount=0.7
else:
adiscount=1
c=Car_General(carid,brand,model,platenum,dayrent,adiscount)
self.cars[carid]=c
if sort_ ==1:
self.stocks['EconomyCar'].append(c)
if sort_ ==2:
self.stocks['LuxuryCar'].append(c)
if sort_ ==3:
self.stocks['SportsCar'].append(c)
if sort_ ==4:
self.stocks['SUV'].append(c)
def rent_car(self):
print(f'1.经济车型(享有补贴),共有{len(self.stocks["EconomyCar"])}辆。\n')
if self.stocks['EconomyCar']:
for each in self.stocks['EconomyCar']:
print(f'车辆编号:{each.carid},品牌:{each.brand},型号:{each.model},日租金:{each.dayrent}-{each.bonus}(补贴)元\n')
print(f'2.豪华车型(需额外购买保险),共有{len(self.stocks["LuxuryCar"])}辆。\n')
if self.stocks['LuxuryCar']:
for each in self.stocks['LuxuryCar']:
print(f'车辆编号:{each.carid},品牌:{each.brand},型号:{each.model},日租金:{each.dayrent}+{each.insurance}(保险)元\n')
print(f'3.跑车(需增加损耗费用),共有{len(self.stocks["SportsCar"])}辆。\n')
if self.stocks['SportsCar']:
for each in self.stocks['SportsCar']:
print(f'车辆编号:{each.carid},品牌:{each.brand},型号:{each.model},日租金:{each.dayrent}+{each.loss}(损耗)元\n')
print(f'4.SUV(租赁超过7天,享有额外折上7折优惠),共有{len(self.stocks["SUV"])}辆。\n')
if self.stocks['SUV']:
for each in self.stocks['SUV']:
print(f'车辆编号:{each.carid},品牌:{each.brand},型号:{each.model},日租金:{each.dayrent}元\n')
idins=int(input('请输入需要租赁的车辆编号:'))
day=int(input('请输入需要租赁的天数:'))
total=cars[idins].calc_rent(day)
if idins.starswith('1'):
self.stocks['EconomyCar'].remove(cars[idins])
if idins.startswith('2'):
self.stocks['LuxuryCar'].remove(cars[idins])
if idins.startswith('3'):
self.stocks['SportsCar'].remove(cars[idins])
if idins.startswith('4'):
if day>7:
total=total*0.7
self.stocks['SUV'].remove(cars[idins])
else:
self.stocks['SUV'].remove(cars[idins])
print(f'租赁{day}天,总共需要花费:{total}元')
print('恭喜,租赁成功~')
def return_car(self):
idins=int(input('请输入车辆编号:'))
if idins.starswith('1'):
self.stocks['EconomyCar'].append(cars[idins])
if idins.startswith('2'):
self.stocks['LuxuryCar'].append(cars[idins])
if idins.startswith('3'):
self.stocks['SportsCar'].append(cars[idins])
if idins.startswith('4'):
self.stocks['SUV'].append(cars[idins])
以上代码运行之后,报出如下错误:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
main.operate()
File "F:\python\小甲鱼\064 类和对象七\064 动动手 - 2.0.py", line 51, in operate
self.register()
File "F:\python\小甲鱼\064 类和对象七\064 动动手 - 2.0.py", line 72, in register
c=Car_General(carid,brand,model,platenum,dayrent,bonus)
TypeError: Car_General.__init__() takes 6 positional arguments but 7 were given
原因为何以及如何修改
本帖最后由 sfqxx 于 2025-2-22 08:21 编辑
错误的原因在于 Car_General 类的 __init__ 方法只接受 5 个参数(self、carid、brand、model、platenum、dayrent),但在 register 方法中,你尝试传递了额外的参数(如 bonus、insurance、loss 等),导致参数数量不匹配。
try this:class Car_General:
def __init__(self, carid, brand, model, platenum, dayrent, bonus=0, insurance=0, loss=0, adiscount=1):
self.brand = brand
self.model = model
self.platenum = platenum
self.dayrent = dayrent
self.carid = carid
self.bonus = bonus
self.insurance = insurance
self.loss = loss
self.adiscount = adiscount
def get_brand(self):
return self.brand
def get_model(self):
return self.model
def get_platenum(self):
return self.platenum
def get_dayrent(self):
return self.dayrent
def get_carid(self):
return self.carid
def calc_rent(self, day, adiscount=1):
self.day = day
self.adiscount = adiscount
self.rent = self.day * (self.dayrent - self.bonus + self.insurance + self.loss) * self.adiscount
return self.rent
class EconomyCar(Car_General):
def __init__(self, carid, brand, model, platenum, dayrent, bonus):
super().__init__(carid, brand, model, platenum, dayrent, bonus=bonus)
class LuxuryCar(Car_General):
def __init__(self, carid, brand, model, platenum, dayrent, insurance):
super().__init__(carid, brand, model, platenum, dayrent, insurance=insurance)
class SportsCar(Car_General):
def __init__(self, carid, brand, model, platenum, dayrent, loss):
super().__init__(carid, brand, model, platenum, dayrent, loss=loss)
class SUV(Car_General):
def __init__(self, carid, brand, model, platenum, dayrent, adiscount=1):
super().__init__(carid, brand, model, platenum, dayrent, adiscount=adiscount)
class CarOperation:
cars = {}
stocks = {'EconomyCar': [], 'LuxuryCar': [], 'SportsCar': [], 'SUV': []}
carid1 = 10000
carid2 = 20000
carid3 = 30000
carid4 = 40000
def operate(self):
print('欢迎使用鱼C汽车租赁程序\n')
while True:
op = int(input('1.录入汽车;2.租车服务;3.还车服务;4.退出程序:\n'))
if op == 1:
self.register()
elif op == 2:
self.rent_car()
elif op == 3:
self.return_car()
elif op == 4:
break
def register(self):
sort_ = int(input('1.经济型车;2.豪华车;3.跑车;4.SUV:'))
num = int(input('请输入需要录入的数量:'))
for i in range(num):
print(f'请录入第{i+1}辆车\n')
brand = input('品牌:')
model = input('型号:')
platenum = input('车牌:')
dayrent = int(input('租金:'))
if sort_ == 1:
bonus = int(input('补贴:'))
carid = self.carid1
self.carid1 += 1
c = EconomyCar(carid, brand, model, platenum, dayrent, bonus)
elif sort_ == 2:
insurance = int(input('保险:'))
carid = self.carid2
self.carid2 += 1
c = LuxuryCar(carid, brand, model, platenum, dayrent, insurance)
elif sort_ == 3:
loss = int(input('损耗:'))
carid = self.carid3
self.carid3 += 1
c = SportsCar(carid, brand, model, platenum, dayrent, loss)
elif sort_ == 4:
carid = self.carid4
self.carid4 += 1
adiscount = 0.7 if int(input('租赁天数:')) > 7 else 1
c = SUV(carid, brand, model, platenum, dayrent, adiscount)
self.cars[carid] = c
if sort_ == 1:
self.stocks['EconomyCar'].append(c)
elif sort_ == 2:
self.stocks['LuxuryCar'].append(c)
elif sort_ == 3:
self.stocks['SportsCar'].append(c)
elif sort_ == 4:
self.stocks['SUV'].append(c)
def rent_car(self):
print(f'1.经济车型(享有补贴),共有{len(self.stocks["EconomyCar"])}辆。\n')
if self.stocks['EconomyCar']:
for each in self.stocks['EconomyCar']:
print(f'车辆编号:{each.carid},品牌:{each.brand},型号:{each.model},日租金:{each.dayrent}-{each.bonus}(补贴)元\n')
print(f'2.豪华车型(需额外购买保险),共有{len(self.stocks["LuxuryCar"])}辆。\n')
if self.stocks['LuxuryCar']:
for each in self.stocks['LuxuryCar']:
print(f'车辆编号:{each.carid},品牌:{each.brand},型号:{each.model},日租金:{each.dayrent}+{each.insurance}(保险)元\n')
print(f'3.跑车(需增加损耗费用),共有{len(self.stocks["SportsCar"])}辆。\n')
if self.stocks['SportsCar']:
for each in self.stocks['SportsCar']:
print(f'车辆编号:{each.carid},品牌:{each.brand},型号:{each.model},日租金:{each.dayrent}+{each.loss}(损耗)元\n')
print(f'4.SUV(租赁超过7天,享有额外折上7折优惠),共有{len(self.stocks["SUV"])}辆。\n')
if self.stocks['SUV']:
for each in self.stocks['SUV']:
print(f'车辆编号:{each.carid},品牌:{each.brand},型号:{each.model},日租金:{each.dayrent}元\n')
idins = int(input('请输入需要租赁的车辆编号:'))
day = int(input('请输入需要租赁的天数:'))
total = self.cars[idins].calc_rent(day)
if str(idins).startswith('1'):
self.stocks['EconomyCar'].remove(self.cars[idins])
elif str(idins).startswith('2'):
self.stocks['LuxuryCar'].remove(self.cars[idins])
elif str(idins).startswith('3'):
self.stocks['SportsCar'].remove(self.cars[idins])
elif str(idins).startswith('4'):
if day > 7:
total = total * 0.7
self.stocks['SUV'].remove(self.cars[idins])
print(f'租赁{day}天,总共需要花费:{total}元')
print('恭喜,租赁成功~')
def return_car(self):
idins = int(input('请输入车辆编号:'))
if str(idins).startswith('1'):
self.stocks['EconomyCar'].append(self.cars[idins])
elif str(idins).startswith('2'):
self.stocks['LuxuryCar'].append(self.cars[idins])
elif str(idins).startswith('3'):
self.stocks['SportsCar'].append(self.cars[idins])
elif str(idins).startswith('4'):
self.stocks['SUV'].append(self.cars[idins])
main = CarOperation()
main.operate()
有用请设置最佳答案谢谢
|
|