Python帮我写一个程序 模拟火车订票系统
时间: 2024-10-12 16:10:01 浏览: 24
用Python写一个学生管理系统小程序
当然可以,下面是一个简单的Python模拟火车订票系统的例子,使用类和命令行交互来实现基本功能:
```python
class TrainTicketSystem:
def __init__(self):
self.train_list = {} # 存储火车信息字典
def add_train(self, train_name, seats):
self.train_list[train_name] = seats
def book_ticket(self, train_name, seat_type, passenger_name):
if train_name in self.train_list:
available_seats = self.train_list[train_name][seat_type]
if available_seats > 0:
print(f"{passenger_name}成功预订了{train_name}列车的{seat_type}座位")
self.train_list[train_name][seat_type] -= 1
return True
else:
print(f"{train_name}列车的{seat_type}座位已售罄")
else:
print(f"没有找到{train_name}列车的信息")
return False
def show_tickets(self):
for train, seats in self.train_list.items():
print(f"火车名:{train}, 座位情况:{seats}")
# 使用示例
ticket_system = TrainTicketSystem()
ticket_system.add_train("和谐号", {"硬座": 50, "软卧": 20})
ticket_system.book_ticket("和谐号", "硬座", "张三")
ticket_system.show_tickets()
#
阅读全文