-1

Complete 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 2018

where 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: 5770

Note: print_info() should use three spaces for indentation.

Code that was provided below

class Car:
    def __init__(self):
        self.model_year = 0
        # TODO: Declare purchase_price attribute
      
        self.current_value = 0

    def calc_current_value(self, current_year):
        depreciation_rate = 0.15
        # Car depreciation formula
        car_age = current_year - self.model_year
        self.current_value = round(self.purchase_price * (1 - depreciation_rate) ** car_age)
    
    # TODO: Define print_info() method to output model_year, purchase_price, and current_value


if __name__ == "__main__":    
    year = int(input()) 
    price = int(input())
    current_year = int(input())
    
    my_car = Car()
    my_car.model_year = year
    my_car.purchase_price = price
    my_car.calc_current_value(current_year)
    my_car.print_info()
    

What I tried but I don't know what I'm doing wrong. I keep getting "TypeError: print_info() takes 0 positional arguments but 1 was given."

class Car:
    def __init__(self):
        self.model_year = 0
        # TODO: Declare purchase_price attribute
        self.purchase_price = ''
        self.current_value = 0

    def calc_current_value(self, current_year):
        depreciation_rate = 0.15
        # Car depreciation formula
        car_age = current_year - self.model_year
        self.current_value = round(self.purchase_price * (1 - depreciation_rate) ** car_age)
    
    # TODO: Define print_info() method to output model_year, purchase_price, and current_value
    def print_info():
        print('Car' + "'" + 's', 'information\n')
        print('Model year:', self.model_year)
        print('Purchase price:', self.purchase_price)
        print('Current value:', self.current_value)

if __name__ == "__main__":    
    year = int(input()) 
    price = int(input())
    current_year = int(input())
    
    my_car = Car()
    my_car.model_year = year
    my_car.purchase_price = price
    my_car.calc_current_value(current_year)
    my_car.print_info()

Ash
  • 1
  • 1
  • 1
  • 1
    Methods in Python always receive at least one parameter, a reference to their instance conventionally called `self`. You got it right for `__init__` and for `calc_current_value`, but you left off `self` for `print_info`. – rhurwitz Dec 14 '22 at 05:30
  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please take the [tour] and read about [ask] good questions. Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Dec 14 '22 at 05:34

3 Answers3

1

Corrected an issue with spacing/white spaces in the print output:

def print_info(self):
    print(f"Car's information:")
    print(f"  Model year: {self.model_year}")
    print(f"  Purchase price: ${self.purchase_price}")
    print(f"  Current value: ${self.current_value}")

WAJ
  • 11
  • 2
0

Changes made:- def print_info(): -> def print_info(self):

Code:-

class Car:
    def __init__(self):
        self.model_year = 0
        # TODO: Declare purchase_price attribute
        self.purchase_price = ''
        self.current_value = 0

    def calc_current_value(self, current_year):
        depreciation_rate = 0.15
        # Car depreciation formula
        car_age = current_year - self.model_year
        self.current_value = round(self.purchase_price * (1 - depreciation_rate) ** car_age)
    
    # TODO: Define print_info() method to output model_year, purchase_price, and current_value
    def print_info(self):
        print('Car' + "'" + 's', 'information\n')
        print('Model year:', self.model_year)
        print('Purchase price:', self.purchase_price)
        print('Current value:', self.current_value)

if __name__ == "__main__":    
    year = int(input()) 
    price = int(input())
    current_year = int(input())
    
    my_car = Car()
    my_car.model_year = year
    my_car.purchase_price = price
    my_car.calc_current_value(current_year)
    my_car.print_info()

Output:

2011
16000
2019
Car's information

Model year: 2011
Purchase price: 16000
Current value: 4360
Yash Mehta
  • 2,025
  • 3
  • 9
  • 20
  • Please take some time to refresh [how to write good answers](https://stackoverflow.com/help/how-to-answer). The change you point out, *why* did you make it? What's the reason for that change, and what does it do? Also, you don't need to copy all of the code from the question, only enough to show the change or fix. – Some programmer dude Dec 14 '22 at 05:35
0
def print_info(self):
    self.print_info = print(f'Car Information:\n Model year: {self.model_year}\n Purchase price: {self.purchase_price}\n Current value: {self.current_value}')
user16217248
  • 3,119
  • 19
  • 19
  • 37