puthon出租车计费
时间: 2023-09-12 18:07:41 浏览: 138
Python出租车计费可以根据计费规则进行编写。一般的计费规则是:起步价(如10元)+里程费(如每公里2元)+时长费(如每分钟0.5元)。以下是一个简单的出租车计费程序示例:
```python
def taxi_cost(distance, time):
start_price = 10 # 起步价
distance_price = 2 # 每公里里程费
time_price = 0.5 # 每分钟时长费
if distance <= 0 or time <= 0:
return 0
# 计算里程费和时长费
distance_cost = distance * distance_price
time_cost = time * time_price
# 计算总费用
total_cost = start_price + distance_cost + time_cost
return total_cost
```
调用函数时,传入参数为行驶里程和时间长度,即可计算出租车费用:
```python
distance = 5 # 行驶里程为5公里
time = 10 # 行驶时间为10分钟
cost = taxi_cost(distance, time)
print("出租车费用为:", cost, "元")
```
输出结果为:
```
出租车费用为: 25.0 元
```
阅读全文