View the step-by-step solution to:

Question

Make a Vehicle class. The inputs are make , model , year , price , and color , in that order. The values should be

saved in the instance under those names. No defaults should exist for these values; they should all be required.

Implement __repr__ and __str__ exactly as shown here: 


Screen Shot 2020-02-11 at 1.05.13 PM.png


I have this so far:


current_year = datetime.datetime.now().year



class Vehicle:

  """A simple class representing a vehicle"""


  def __init__(self, make, model, year, price, color):

    self.make = make

    self.model = model

    self.year = year

    self.price = price

    self.color = color


  def current_value


  def __str__(self):

    return ("This is a {} {} {} {} costing {}".format(self.year,self.color,self.make,self.model,self.price))


  def __repr__(self):

    return "Vehicle('{}', '{}', {}, {}, '{}')".format(self.make,self.model,self.year,self.price,self.color)




ThenI need to add a property current_value that calculates and returns the current value of the vehicle, based the vehicle's age and this completely arbitrary formula:


Use the function datetime.now() , from which the current_year is extracted so you can calculate the age of the vehicle. Because a vehicle loses some value immediately after purchase, calculate the vehicle's age as current_year - self.year + 1 . If the vehicle's age is over 5 years old, then its current value is 10% of the price. Otherwise, its current value is the price minus 15% of the price for each year of age. 


Screen Shot 2020-02-11 at 1.05.24 PM.png


Then add checking to make sure that:

1. The year input is an integer

2. The price is either integer or float


Raise a TypeError if either is not the case. Hint: Use the built-in function isinstance(). The classinfo can be a tuple of more than one type to test; isinstance will return True if the object is one of any of the types in classinfo.


The messages should look like this:

>>> make, model, color = 'Toyota Camry White'.split() >>> car3 = Vehicle(make, model, 201.2, 30000, color) Traceback (most recent call last):

[...]

TypeError: Input year must be integer!

>>> car3 = Vehicle(make, model, 2012, color, color) Traceback (most recent call last):

[...]

TypeError: Input price must be a number! >>>

Note: the [...] represents the traceback information, which is not always the same on different computers. 





Here is what I have so far:


current_year = datetime.datetime.now().year



class Vehicle:

  """A simple class representing a vehicle"""


  def __init__(self, make, model, year, price, color):

    self.make = make

    self.model = model

    self.year = year

    self.price = price

    self.color = color


  def current_value


  def __str__(self):

    return ("This is a {} {} {} {} costing {}".format(self.year,self.color,self.make,self.model,self.price))


  def __repr__(self):

    return "Vehicle('{}', '{}', {}, {}, '{}')".format(self.make,self.model,self.year,self.price,self.color)

Screen Shot 2020-02-11 at 1.05.13 PM.png

>>> from HW4 import Vehicle
>>> car = Vehicle( "Nissan", "Versa", 2018, 25000, "Silver")
> > > car . make
'Nissan'
> > > car . model
'Versa'
> > > car . year
2018
>>> car . price
25000
> > > car . color
'Silver'
> > > repr (car)
'Vehicle ("Nissan", "Versa", 2018, 25000, "Silver") '
> > > car
Vehicle ( "Nissan", "Versa", 2018, 25000, "Silver")
> > > str(car)
'This is a 2018 Silver Nissan Versa costing 25000'
> > > print (car)
This is a 2018 Silver Nissan Versa costing 25000

Screen Shot 2020-02-11 at 1.05.24 PM.png

>>> make, model, color = 'Toyota Camry White'.5plit()
>>> car2 = Vehicle(make, model, 2012, 30000.50, color)
>>> car2 Vehicle("Toyota", ”Camry”, 2012, 30000.5, color)
>>> car2.current_value 3000.05 >>> car3 = Vehicle(make, model, 2020, 30000, color)
>>> car3.current_value 25500.0 >>> car3 = Vehicle(make, model, 2019, 30000, color)
>>> car3.current_value 21000.0 >>> car3 = Vehicle(make, model, 2018, 30000, color)
>>> car3.current_value 16500.0 >>> car3 = Vehicle(make, model, 2017, 30000, color)
>>> car3.current_value 12000.0 >>> car3 = Vehicle(make, model, 2016, 30000, color)
>>> car3.current_value 7500.0 >>> car3 = Vehicle(make, model, 2015, 30000, color)
>>> car3.current_value 3000.0 >>> car3 = Vehicle(make, model, 2012, 30000, color)
>>> car3.current_value 3000.0

Top Answer

Points to consider: We need to calculate the current value... View the full answer

Capture.PNG

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question