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
Someone guide please.
Code:
class vehicle: def __init__(self,name,color,max_speed,mileage): self.name = name self.color = "white" self.max_speed = max_speed self.mileage = mileage class Bus(vehicle): pass class Car(vehicle): pass class Bike(vehicle): pass obj = Bus("volvo",120,4) obj1= Car("Wagnor",180,8) obj2= Bike("pulsar",110,5) print(f"Bus model name is {obj.name} with color {obj.color} and maximum speed is {obj.max_speed}") print(f"Car model name is {obj1.name} with color {obj1.color} and maximum speed is {obj1.max_speed}") print(f"Bike model name is {obj2.name} with color {obj2.color} and maximum speed is {obj2.max_speed}")
output:
TypeError Traceback (most recent call last) Cell In [2], line 14 12 class Bike(vehicle): 13 pass ---> 14 obj = Bus("volvo",120,4) 15 obj1= Car("Wagnor",180,8) 16 obj2= Bike("pulsar",110,5) TypeError: vehicle.__init__() missing 1 required positional argument: 'mileage'
You have taken 4 parameters in init but have passed only 3 check it while creating the objects
The error message is indicating that there is a missing required argument in the constructor of the vehicle class. Specifically, the __init__ method of the vehicle class takes four arguments (name, color, max_speed, and mileage) but when creating the obj object of the Bus class, you are only passing in three arguments (name, max_speed, and mileage). To fix this, you need to pass in a value for the color argument when creating the obj, obj1, and obj2 objects.