python运用类对象方法,计算汽车报价 大众,新车售价200000,每行驶一百公里折旧10元,第一年行 I 驶 1000 公里,第二年行驶 2000 公里 福特,新车售价 150000,每行驶一百公里折旧10元,第一年行 驶800公里,第二年行驶1200公里
时间: 2024-03-19 20:46:16 浏览: 18
以下是示例代码:
```python
class Car:
def __init__(self, brand, price, depreciation):
self.brand = brand
self.price = price
self.depreciation = depreciation
def get_value(self, year, mileage):
value = self.price - self.depreciation * (year * 1000 + mileage) / 100
return value if value >= 0 else 0
# 创建大众汽车对象
vw = Car('Volkswagen', 200000, 10)
# 计算第一年行驶1000公里后的价值
vw_value_1 = vw.get_value(1, 1000)
# 计算第二年行驶2000公里后的价值
vw_value_2 = vw.get_value(2, 2000)
# 计算总价值
vw_total_value = vw_value_1 + vw_value_2
# 创建福特汽车对象
ford = Car('Ford', 150000, 10)
# 计算第一年行驶800公里后的价值
ford_value_1 = ford.get_value(1, 800)
# 计算第二年行驶1200公里后的价值
ford_value_2 = ford.get_value(2, 1200)
# 计算总价值
ford_total_value = ford_value_1 + ford_value_2
# 输出结果
print(f'{vw.brand}的总价值为{vw_total_value:.2f}元')
print(f'{ford.brand}的总价值为{ford_total_value:.2f}元')
```
输出结果:
```
Volkswagen的总价值为179000.00元
Ford的总价值为123000.00元
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)