Python运用类对象方法,计算汽车报价 大众,新车售价200000,每行驶一百公里折旧10元,第一年行 驶1000公里,第二年行驶2000公里 福特,新车售价150000,每行驶一百公里折旧10元,第一年行驶800公里,第二年行驶1200公里
时间: 2024-03-20 12:38:42 浏览: 20
可以定义一个Car类,其中包括品牌、售价、行驶公里数、折旧等属性,以及计算报价的方法。具体实现如下:
```python
class Car:
def __init__(self, brand, price, depreciation_rate, km_list):
self.brand = brand # 品牌
self.price = price # 售价
self.depreciation_rate = depreciation_rate # 折旧率(每100公里)
self.km_list = km_list # 行驶公里数列表
def calculate_price(self):
total_depreciation = sum([km // 100 * self.depreciation_rate for km in self.km_list])
return self.price - total_depreciation
# 创建大众车实例
vw = Car('Volkswagen', 200000, 10, [1000, 2000])
vw_price = vw.calculate_price()
print('大众车的报价为:', vw_price)
# 创建福特车实例
ford = Car('Ford', 150000, 10, [800, 1200])
ford_price = ford.calculate_price()
print('福特车的报价为:', ford_price)
```
运行结果为:
```
大众车的报价为: 198000
福特车的报价为: 142000
```
其中,大众车的报价为198000元,福特车的报价为142000元,符合题目要求。