鱼C论坛

 找回密码
 立即注册
查看: 1064|回复: 3

[已解决]报错

[复制链接]
发表于 2024-1-1 12:43:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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[self.class_] + 1
        number[self.class_] += 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.get_carid] = self.car


实验:main = Caroperation()
main.register('EconomyCar')
品牌:11
型号:11
车牌:11
租金11
补贴:11
[color=Red][color=Red]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'


最佳答案
2024-1-1 12:56:37
45行少了一个class参数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-1-1 12:43:39 | 显示全部楼层
您这段代码在最后一行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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-1-1 12:56:37 | 显示全部楼层    本楼为最佳答案   
45行少了一个class参数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-1-1 13:10:04 | 显示全部楼层
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_
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 21:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表