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:

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.

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)

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

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

Points to consider: We need to calculate the current value... 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