TypeError:__init __()缺少2个必需的位置参数。为什么_init()_在那里缺少参数?

时间:2018-08-10 04:30:30

标签: python oop typeerror

class Vehicle:
    def __init__(self,vehicle_cost,vehicle_type):
        self.__vehicle_id =None
        self.__vehicle_type=vehicle_type
        self.__vehicle_cost=vehicle_cost
        self.__premium_amount=None


    def set_vehicle_id(self,vehicle_id):
        self.__vehicle_id=vehicle_id

    def get_vehicle_id(self):
        return self.__vehicle_id

    def set_vehicle_type(self,vehicle_type):
        self.__vehicle_type=vehicle_type

    def get_vehicle_type(self):
        return self.__vehicle_type

    def set_vehicle_cost(self,vehicle_cost):
        self.__vehicle_cost=vehicle_cost

    def get_vehicle_cost(self):
        return self.__vehicle_cost

    def set_premium_amount(self,premium_amount):
        self.__premium_amount=premium_amount

    def get_premium_amount(self):
        return self.__premium_amount

    def display_vehicle_details(self):
            print("Vehicle Id is",self.__vehicle_id)
            print("Vehicle Type is",self.__vehicle_type)
            print("Vehicle Cost is",self.__vehicle_cost)

    def calculate_premium(self):
        if(self.__vehicle_type =="Two Wheeler"):
            self.__premium_amount=self.__vehicle_cost-self.__vehicle_cost*0.98
            print(self.__premium_amount)
        elif(self.__vehicle_type =="Four Wheeler"):
            self.__premium_amount=self.__vehicle_cost-self.__vehicle_cost*0.94
            print(self.__premium_amount)
        else:
            print("Error: Wrong Vehicle type")


obj=Vehicle(105000, "Two Wheeler")
obj.calculate_premium()

2 个答案:

答案 0 :(得分:0)

我这边没有错误。您的代码正确无误,它给了我2100.0的输出。

尝试重命名文件并创建一个扩展名为.py的新文件,然后在控制台上使用 python filename.py 命令运行,而不是直接在控制台上尝试。

答案 1 :(得分:0)

如果u停止通过init方法传递任何值,因此每个属性都被初始化为None,然后仅依靠set方法将值传递给属性就可以解决问题。

class Vehicle:

    def __init__(self):
        self.__vehicle_id =None
        self.__vehicle_type=None
        self.__vehicle_cost=None
        self.__premium_amount=None


    def set_vehicle_id(self,vehicle_id):
        self.__vehicle_id=vehicle_id

    def get_vehicle_id(self):
        return self.__vehicle_id

    def set_vehicle_type(self,vehicle_type):
        self.__vehicle_type=vehicle_type

    def get_vehicle_type(self):
        return self.__vehicle_type

    def set_vehicle_cost(self,vehicle_cost):
        self.__vehicle_cost=vehicle_cost

    def get_vehicle_cost(self):
        return self.__vehicle_cost

    def set_premium_amount(self,premium_amount):
        self.__premium_amount=premium_amount

    def get_premium_amount(self):
        return self.__premium_amount

    def display_vehicle_details(self):
        print("Vehicle Id is",self.__vehicle_id)
        print("Vehicle Type is",self.__vehicle_type)
        print("Vehicle Cost is",self.__vehicle_cost)

    def calculate_premium(self):
        if(self.__vehicle_type =="Two Wheeler"):
            self.__premium_amount=self.__vehicle_cost-self.__vehicle_cost*0.98
            print(self.__premium_amount)
        elif(self.__vehicle_type =="Four Wheeler"):
            self.__premium_amount=self.__vehicle_cost-self.__vehicle_cost*0.94
            print(self.__premium_amount)
        else:
            print("Error: Wrong Vehicle type")