python简易停车场收费代码
时间: 2023-06-10 20:05:22 浏览: 293
好的,以下是一个简单的停车场收费 Python 代码示例:
```python
class ParkingLot:
def __init__(self, spots):
self.total_spots = spots
self.available_spots = spots
self.parking_slots = {}
def park(self, reg_number):
if self.available_spots == 0:
print("Sorry, parking lot is full.")
return False
else:
slot = self.total_spots - self.available_spots + 1
self.available_spots -= 1
print(f"Allocated slot number: {slot}")
self.parking_slots[slot] = reg_number
return True
def leave(self, slot):
if slot not in self.parking_slots:
print("Invalid slot number.")
return False
else:
self.available_spots += 1
del self.parking_slots[slot]
print(f"Slot number {slot} is free.")
return True
def status(self):
print("Slot No.\tRegistration No.")
for slot, reg_num in self.parking_slots.items():
print(f"{slot}\t\t{reg_num}")
def get_reg_numbers_for_color(self, color):
reg_numbers = []
for slot, reg_num in self.parking_slots.items():
if self.parking_slots[slot] == color:
reg_numbers.append(reg_num)
if reg_numbers:
print(", ".join(reg_numbers))
else:
print("Not found.")
def get_slot_numbers_for_color(self, color):
slot_numbers = []
for slot, reg_num in self.parking_slots.items():
if self.parking_slots[slot] == color:
slot_numbers.append(str(slot))
if slot_numbers:
print(", ".join(slot_numbers))
else:
print("Not found.")
def get_slot_number_for_reg_number(self, reg_number):
for slot, num in self.parking_slots.items():
if self.parking_slots[slot] == reg_number:
print(f"{slot}")
return
print("Not found.")
def main():
capacity = input("Enter parking lot capacity: ")
parking_lot = ParkingLot(int(capacity))
while True:
command = input("Enter command: ")
inputs = command.split()
if inputs[0] == "park":
parking_lot.park(inputs[1])
elif inputs[0] == "leave":
parking_lot.leave(int(inputs[1]))
elif inputs[0] == "status":
parking_lot.status()
elif inputs[0] == "registration_numbers_for_cars_with_colour":
parking_lot.get_reg_numbers_for_color(inputs[1])
elif inputs[0] == "slot_numbers_for_cars_with_colour":
parking_lot.get_slot_numbers_for_color(inputs[1])
elif inputs[0] == "slot_number_for_registration_number":
parking_lot.get_slot_number_for_reg_number(inputs[1])
elif inputs[0] == "exit":
break
else:
print("Invalid input")
if __name__ == "__main__":
main()
```
该停车场有以下功能:
- park <reg_number>:停车并分配空闲车位
- leave <slot>:从指定车位上离开车辆
- status:查看停车场状态,包括所有已占用车位的车牌号码和车位号码
- registration_numbers_for_cars_with_colour <color>:查找与指定颜色匹配的所有车牌号码
- slot_numbers_for_cars_with_colour <color>:查找与指定颜色匹配的所有车位号码
- slot_number_for_registration_number <reg_number>:查找与指定车牌号码匹配的车位号码
当输入 “exit” 时程序停止运行。
阅读全文