199k views
4 votes
I am having trouble with doing this code on Python:Develop the Car class by creating an attribute purchase_price (type int) and the method print_info() that outputs the car's information.Ex: If the input is:2011 18000 2018where 2011 is the car's model year, 18000 is the purchase price, and 2018 is the current year, then print_info() outputs:Car's information: Model year: 2011 Purchase price: 18000 Current value: 5770Note: print_info() should use three spaces for indentation.

1 Answer

5 votes

Answer:

class Car:

def __init__(self, model_year, purchase_price):

self.model_year = model_year

self.purchase_price = purchase_price

self.current_year = 0

self.current_value = 0

def set_current_year(self, year):

self.current_year = year

def calc_current_value(self, current_year):

if self.current_year == 0:

return "Must first input the current year"

depreciation_rate = 0.15

car_age = current_year - self.model_year

self.current_value = round(self.purchase_price - (car_age * (self.purchase_price * 0.15)))

def print_info(self):

print("Car's information:")

print(f"Model year: {self.model_year}")

print(f"Purchase price: {self.purchase_price}")

if self.current_value > 0:

print(f"Current value: {self.current_value}")

else:

print("Current value: Unknown")

Step-by-step explanation:

The python program above defines the class "Car" that accepts the model year and purchase price of an instance. The class has a set method for the current year variable that is used to calculate and update the current value variable of the object. The print_info method prints the car model year, purchase price and its current value after nth years.

User Alizx
by
5.6k points