Modify the PrintCarInfo.py program's _str__() method so that printing an instance of Car displays a string in the following format:
1989 Chevrolet Blazer:
Mileage: 115000
Sticker price: $3250
class Car:
def __init__(self, make, model, year, miles,
price):
self.make = make
self.model = model
self.year = year
self.miles = miles
self.price = price
def __str__(self):
return ('\n%s %s %s : \n
Mileage: %d \n Sticker Price: $%.2f.' %
(self.year, self.make, self.model, self.miles, self.price))
cars = []
cars.append(Car('Ford', 'Mustang', 2013, 25000, 37999))
cars.append(Car('Nissan', 'Xterra', 2004, 89500, 7500))
cars.append(Car('Nissan', 'Mazda', 2012, 25000, 15750))
for car in cars:
print(car)
Program:
class Car:
def __init__(self, make, model, year, miles,
price):
self.make = make
self.model = model
self.year = year
self.miles = miles
self.price =
price
def __str__(self):
return ('%s %s
%s:\nMileage: %d\nSticker price: $%d' %
(self.year, self.make, self.model, self.miles,
self.price))
cars = []
cars.append(Car('Ford', 'Mustang', 2013, 25000, 37999))
cars.append(Car('Nissan', 'Xterra', 2004, 89500, 7500))
cars.append(Car('Nissan', 'Mazda', 2012, 25000, 15750))
for car in cars:
print("")
print(car)
print("")
--> Screenshot of the code for reference
Output:
Note: If you have any doubts please comment.
It will be great help If you like.
Modify the PrintCarInfo.py program's _str__() method so that printing an instance of Car displays a string...