All Courses

Getting this error...pls explain

By Manerushi149@gmail.com, 3 months ago
  • Bookmark
0

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

Error
Python
Oops
2 Answers
0
Hitendradixit18@gmail.com

"car" extra in argument

0
Goutamp777

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'

Your Answer

Webinars

Live Masterclass on : "How Machine Get Trained in Machine Learning?"

Mar 30th (7:00 PM) 516 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More