写一个出租车计费功能
时间: 2024-10-23 08:08:08 浏览: 29
基于QuartusII的出租车计费系统.docx
5星 · 资源好评率100%
编写一个简单的出租车计费功能,通常会基于起步价、里程费和额外费用(如等待时间和夜间服务费)来计算总费用。假设基础价格包括固定起步价和每公里的基本里程费。这里是一个基本的Python示例:
```python
class Taxi:
def __init__(self, base_price, per_km_rate, wait_time_fee=0, night_time_fee=0):
self.base_price = base_price
self.per_km_rate = per_km_rate
self.wait_time_fee = wait_time_fee
self.night_time_fee = night_time_fee
def calculate_fare(self, distance, wait_minutes=0, is_night=False):
# 起步价
fare = self.base_price
# 行驶距离费用
fare += distance * self.per_km_rate
# 等待时间费用(如果有)
if wait_minutes > 0:
fare += self.wait_time_fee
# 夜间服务费用(如果适用)
if is_night:
fare += self.night_time_fee
return fare
# 使用示例
taxi = Taxi(10, 2) # 起步价10元,每公里2元
distance = 15 # 15公里
wait_minutes = 10 # 等待10分钟
is_night = True # 晚上
fare = taxi.calculate_fare(distance, wait_minutes, is_night)
print(f"车费总计: {fare}元")
阅读全文