通过创建属性 purchase_price(int 类型)和输出汽车信息的方法 print_info() 来完成 Car 类。
Ex:如果输入是: 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() 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()
所做的更改:-
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
10/10
def print_info(self):
self.print_info = print(f'Car\'s information:\n Model year: {self.model_year}\n Purchase price: {self.purchase_price}\n Current value: {self.current_value}')