报错
number = {'Economy':9999, 'LuxuryCar':19999, 'SportCar':29999, 'SUV':39999}class Cars:
def __init__(self, class_, brand, model, platenum, dayrent, loss, discount):
self.brand = brand
self.model = model
self.platenum = platenum
self.dayrent = dayrent
self.class_ = class_
if self.class_ == 'EconomyCar' or self.class_ == 'SUV':
self.discount = discount
else:
self.loss = loss
def count_price(self, days):
if self.class_ == 'EconomyCar':
self.count = (self.dayrent - self.discount) * days
elif self.class_ == 'LuxuryCar':
self.count = (self.dayrent + self.loss) * days
elif self.class_ == 'SportCar':
self.count = (self.dayrent + self.loss) * days
else:
if days <= 7:
count = (self.dayrent - self.discount) * days
else:
count = (self.dayrent - self.discount) * days * 0.7
return self.count
def get_carid(self):
self.carid = number + 1
number += 1
return self.carid
class Caroperation:
def __init__(self):
self.cars = {}
def register(self, class_):
self.brand = input("品牌:")
self.model = input("型号:")
self.platenum = input("车牌:")
self.dayrent = input("租金")
if class_ == 'EconomyCar' or class_ == 'SUV':
if class_ == 'EconomyCar':
self.discount = input("补贴:")
else:
self.discount = input("折扣:")
self.car = Cars(self.brand, self.model, self.platenum, self.dayrent, None, self.discount)
else:
if class_ == 'LuxuryCar':
self.loss = input("保险:")
else:
self.loss = input("损耗:")
self.car = Cars(self.brand, self.model, self.platenum, self.dayrent, self.loss, None)
self.cars = self.car
实验:main = Caroperation()
main.register('EconomyCar')
品牌:11
型号:11
车牌:11
租金11
补贴:11
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
main.register('EconomyCar')
File "C:/Users/WL/OneDrive/桌面/12.py", line 45, in register
self.car = Cars(self.brand, self.model, self.platenum, self.dayrent, None, self.discount)
TypeError: Cars.__init__() missing 1 required positional argument: 'discount'
您这段代码在最后一行if语句的条件判断部分缺少了一个比较符号。如果您想要判断days是否大于等于7天,应该写成`if days >= 7:`。
完整代码如下:
number = {'Economy':9999, 'LuxuryCar':19999, 'SportCar':29999, 'SUV':39999}
class Cars:
def __init__(self, class_, brand, model, platenum, dayrent, loss, discount):
self.brand = brand
self.model = model
self.platenum = platenum
self.dayrent = dayrent
self.class_ = class_
if self.class_ == 'EconomyCar' or self.class_ == 'SUV':
self.discount = discount
else:
self.loss = loss
def count_price(self, days):
if self.class_ == 'EconomyCar':
self.count = (self.dayrent - self.discount) * days
elif self.class_ == 'LuxuryCar':
self.count = (self.dayrent + self.loss) * days
elif self.class_ == 'SportCar':
self.count = (self.dayrent + self.loss) * days
else:
if days >= 7:# 添加了比较符号
self.count = (self.dayrent * days) * 0.9
else:
self.count = self.dayrent * days
希望能帮到您!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 45行少了一个class参数 class Cars:
def __init__(self, class_, brand, model, platenum, dayrent, loss, discount): # 需要提供 7 个输入参数
. . . . . .
def register(self, class_):
. . . . . .
self.car = Cars(self.brand, self.model, self.platenum, self.dayrent, None, self.discount)# 【错误】:只提供了 6 个输入参数,丢掉了第一个参数 class_
. . . . . .
self.car = Cars(self.brand, self.model, self.platenum, self.dayrent, self.loss, None) # 【错误】:只提供了 6 个输入参数,丢掉了第一个参数 class_
页:
[1]