Question: Q 5 - Car Inventory ( 1 . 4 points ) For this question you will create an object to store information on an inventory
Q Car Inventory points
For this question you will create an object to store information on an inventory of cars.
Create a class called CarInventory
Instance Attibutes:
ncars : intcars : list
Methods
addcarcompare
Details:
The constructor init should take no inputs, but set ncars to and cars to be an empty list
Method: addcar
Inputs:
manufacturer : string
model : string
year : int
mpg : float
Procedures:
Create a dictionary from the values passed with the corresponding keys 'manufacturer', 'model', 'year' and mpg
Append this dictionary to the cars attribute
Increment the ncars attribute by
Method: compare
This method will compare all the cars on a specified attribute year or mpg and find the highest and lowest values. It will return either the highest of lowest value, depending on the setting in a parameter direction.
Inputs:
attribute : string
direction : string, default: 'highest'
Procedures:
Initialize a variable called lowest to be the th element of cars, and another variable highest to be the same
Loop through each car in cars
If the value of the attribute for the car is less than the car stored as lowest, replace lowest to be the current carSimilarly, if the value of the attribute for the car is greater than the car stored as highest, replace highest to be the current car
After the loop, if the value of direction is 'highest', set a new variable output to be the variable highest
Else if the value of direction is 'lowest', set output to be lowest
Return output
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
