python做停车场计费系统代码
时间: 2023-10-25 08:34:48 浏览: 148
好的,下面是一个简单的Python停车场计费系统代码:
```python
import datetime
class ParkingLot:
def __init__(self):
self.spaces = {}
def park(self, car_number):
if car_number not in self.spaces:
self.spaces[car_number] = datetime.datetime.now()
print(f"Car {car_number} parked at {self.spaces[car_number]}.")
else:
print(f"Car {car_number} is already parked.")
def unpark(self, car_number):
if car_number in self.spaces:
time_elapsed = datetime.datetime.now() - self.spaces[car_number]
hours_elapsed = time_elapsed.total_seconds() / 3600
cost = round(hours_elapsed * 5, 2)
del self.spaces[car_number]
print(f"Car {car_number} unparked. Total cost: ${cost}.")
else:
print(f"Car {car_number} is not parked here.")
parking_lot = ParkingLot()
parking_lot.park("ABC123")
parking_lot.park("DEF456")
parking_lot.unpark("ABC123")
parking_lot.unpark("GHI789")
```
这个代码使用了Python的datetime模块来计算停车时间和费用。停车场对象ParkingLot有一个spaces字典,用来存储每个车辆的停车时间。当一个车辆进入停车场时,它的车牌号被添加到spaces中,并且打印出停车时间。当一个车辆离开停车场时,它的车牌号从spaces中删除,并且计算出停车时间和费用。费用按每小时5美元计算。如果一个车辆没有在停车场,输出一条错误信息。
阅读全文