Question

Modify the PrintCarInfo.py program's _str__() method so that printing an instance of Car displays a string...

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)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
Modify the PrintCarInfo.py program's _str__() method so that printing an instance of Car displays a string...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT