使用Python运用类对象方法,计算汽车报价 大众,新车售价200000,每行驶一百公里折旧10元,第一年行 驶1000公里,第二年行驶2000公里 福特,新车售价150000,每行驶一百公里折旧10元,第一年行 驶800公里,第二年行驶1200公里
时间: 2024-03-20 12:39:05 浏览: 18
抱歉,我发现我之前的回答有误。下面是修改后的代码:
```python
class Car:
def __init__(self, brand, price, mileage_rate, year1_mileage, year2_mileage):
self.brand = brand
self.price = price
self.mileage_rate = mileage_rate
self.year1_mileage = year1_mileage
self.year2_mileage = year2_mileage
def calculate_depreciation(self, mileage):
return mileage * self.mileage_rate
def calculate_quote(self, year):
if year == 1:
mileage = self.year1_mileage
elif year == 2:
mileage = self.year2_mileage
else:
return "Invalid year"
depreciation = self.calculate_depreciation(mileage)
return self.price - depreciation
volkswagen = Car("Volkswagen", 200000, 10, 1000, 2000)
ford = Car("Ford", 150000, 10, 800, 1200)
vw_quote_year1 = volkswagen.calculate_quote(1)
vw_quote_year2 = volkswagen.calculate_quote(2)
ford_quote_year1 = ford.calculate_quote(1)
ford_quote_year2 = ford.calculate_quote(2)
print("Year 1 Quote for Volkswagen: ", vw_quote_year1)
print("Year 2 Quote for Volkswagen: ", vw_quote_year2)
print("Year 1 Quote for Ford: ", ford_quote_year1)
print("Year 2 Quote for Ford: ", ford_quote_year2)
```
运行结果如下:
```
Year 1 Quote for Volkswagen: 190000
Year 2 Quote for Volkswagen: 180000
Year 1 Quote for Ford: 142000
Year 2 Quote for Ford: 138000
```
在这个代码中,我们定义了一个 `Car` 类,包含了汽车的品牌、价格、每行驶一百公里折旧金额、第一年和第二年的里程。`calculate_depreciation` 方法用于根据传入的里程计算折旧金额,`calculate_quote` 方法用于计算汽车的报价,其中需要传入年份参数。在 `calculate_quote` 方法中,我们通过年份来获取相应的里程,然后计算折旧金额,最后用新车售价减去折旧金额得出报价。最后我们创建了两个汽车实例 Volkswagen 和 Ford,并调用它们的 `calculate_quote` 方法来计算报价。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)