146k views
0 votes
Complete the class definition.

class vehicle:
def __init__(self,strModel,strColor):
self.model = strModel
self.color = strColor


def __str__(self):

print(self.model)
print(self.color)

myCar = vehicle('SUV','red')
myCar.display()

User Gizzmo
by
7.1k points

2 Answers

0 votes

Final answer:

The class definition for a vehicle needs to return a string in the __str__ method instead of printing it. Additionally, a display method should be added to properly allow myCar.display() to function.

Step-by-step explanation:

The class definition provided contains a constructor (__init__) method to initialize the model and color attributes of the vehicle class and a print statement in the __str__ method which needs to be corrected. The __str__ method should return a string rather than printing it. Additionally, there's a call to a display method that isn't defined in the class. Below is the corrected class definition with a display method added:

class vehicle:
def __init__(self, strModel, strColor):
self.model = strModel
self.color = strColor

def __str__(self):
return 'Model: ' + self.model + ', Color: ' + self.color

def display(self):
print(str(self))

With these corrections, when you create an instance of the vehicle class and call the display method, like with myCar, it will correctly print the model and color of the vehicle.


User Chaunte
by
7.4k points
0 votes

Answer:

class Vehicle:

def __init__(self, strModel, strColor):

self.model = strModel

self.color = strColor

def __str__(self):

return f"{self.model} {self.color}”

#you can’t just use print statements

myCar = Vehicle('SUV’, ‘red')

"""you haven’t created a display() method, instead, you can use the __str__() method that you have created and when calling, DONT do myCar.__str__(), instead just use print(), since the print() == __str__()

"""

print(myCar);

User Krisna
by
6.3k points