World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
class Vehicle: def __init__(self,max_speed,mileage): self.max_speed = max_speed self.mileage = mileage def display(self): print(f'max_speed - {self.max_speed} and mileage - {self.mileage}') car = Vehicle("car",45,15) car.display()
output:
TypeError Traceback (most recent call last) Cell In [1], line 7 5 def display(self): 6 print(f'max_speed - {self.max_speed} and mileage - {self.mileage}') ----> 7 car = Vehicle("car",45,15) 8 car.display() TypeError: Vehicle.__init__() takes 3 positional arguments but 4 were given
This error is occurring because in the line "car = Vehicle("car",45,15)", you are passing in 4 arguments to the init constructor, while the constructor only expects 3 arguments. Specifically, the first argument is being interpreted as the "self" parameter, the second is "max_speed", the third is "mileage", but you added an additional argument "car" which it does not expect.
You may have wanted to use first argument for "name" and use it in the display method. And you could add this as additional argument in constructor too.
class Vehicle: def __init__(self,name,max_speed,mileage): self.name = name self.max_speed = max_speed self.mileage = mileage def display(self): print(f'Name- {self.name}, max_speed - {self.max_speed} and mileage - {self.mileage}') car = Vehicle("car",45,15) car.display()
And this will run without any errors and prints
'Name- car, max_speed - 45 and mileage - 15'