All Courses

Getting this error

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

Getting this error...pls explain 

Code:

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 [3], 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
Typeerror:
Python
Oops
2 Answers
0
Hitendradixit18@gmail.com

"car" extra in argument

0
Goutamp777

This code is attempting to create an instance of the Vehicle class, but it is passing the wrong number of arguments to the class's constructor (init method). The constructor is defined to take two arguments (max_speed and mileage) but three arguments are being passed when the instance is created on line 7.

( "car", 45, 15)

It should be changed to

car = Vehicle(45,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