python 8.9 LAB:汽车价值(类)

3hvapo4f  于 2023-08-02  发布在  Python
关注(0)|答案(3)|浏览(96)

通过创建属性purchase_price(类型int)和输出汽车信息的方法print_info()来完成Car类。
例如:如果输入为:2011年18000 2018年
其中2011年是汽车的车型年份,18000是购买价格,2018年是当前年份,则print_info()输出:汽车信息:车型年份:2011年购买价格:18000当前值:5770
注意:print_info()应该使用三个空格进行缩进。
下面提供的代码

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()

字符串
我尽力了但我不知道我做错了什么。我一直得到“TypeError:print_info()接受0个位置参数,但给出了1个。”

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()

monwx1rj

monwx1rj1#

更改:-def print_info(): -> def print_info(self):
验证码:-

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()

字符串

输出:

2011
16000
2019
Car's information

Model year: 2011
Purchase price: 16000
Current value: 4360

lf5gs5x2

lf5gs5x22#

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}')

字符串

hgncfbus

hgncfbus3#

更正了打印输出中的间距/白色问题:

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}")

字符串

相关问题