通过创建属性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()
型
3条答案
按热度按时间monwx1rj1#
更改:-
def print_info(): -> def print_info(self):
验证码:-
字符串
输出:
型
lf5gs5x22#
字符串
hgncfbus3#
更正了打印输出中的间距/白色问题:
字符串