只用python写出一个简易火车售票系统,车辆等信息要用字典输入,要有注册,浏览,购买,改签和退票的功能,要显示多个车次每个车次要有不同的座位,不同的价格,可以一次性购买多张票,要有余票限制,车次时间,和起点站,终点站,购买完成之后要显示订单,代码
时间: 2024-03-01 20:55:04 浏览: 221
python 订票系统
5星 · 资源好评率100%
好的,我会尽力来写一个简易的火车售票系统,以下是代码:
```python
# 火车售票系统
# 车辆信息
trains = {'G1': {'time': '8:00', 'start': '北京', 'end': '上海', 'price': 1000, 'seats': 100},
'G2': {'time': '9:00', 'start': '上海', 'end': '北京', 'price': 1000, 'seats': 100},
'G3': {'time': '10:00', 'start': '北京', 'end': '广州', 'price': 1500, 'seats': 50},
'G4': {'time': '11:00', 'start': '广州', 'end': '北京', 'price': 1500, 'seats': 50},
'G5': {'time': '12:00', 'start': '北京', 'end': '深圳', 'price': 2000, 'seats': 20},
'G6': {'time': '13:00', 'start': '深圳', 'end': '北京', 'price': 2000, 'seats': 20}}
# 用户信息
users = []
# 注册函数
def register():
name = input("请输入您的姓名:")
id_card = input("请输入您的身份证号:")
user = {'name': name, 'id_card': id_card, 'orders': []}
users.append(user)
print("注册成功!")
# 浏览函数
def show_trains():
for train in trains:
print(train, "出发时间:", trains[train]['time'], "起点:", trains[train]['start'],
"终点:", trains[train]['end'], "票价:", trains[train]['price'], "余票:", trains[train]['seats'])
# 购买函数
def buy_ticket(user):
train_id = input("请输入车次:")
if train_id not in trains:
print("输入错误!")
return
train = trains[train_id]
seat_count = int(input("请输入购票数量:"))
if seat_count > train['seats']:
print("余票不足!")
return
total_price = train['price'] * seat_count
if input("总价为{}元,确认购买?(y/n)".format(total_price)) != 'y':
print("取消购买!")
return
for i in range(seat_count):
order = {'train_id': train_id, 'start': train['start'], 'end': train['end'], 'price': train['price']}
user['orders'].append(order)
trains[train_id]['seats'] -= 1
print("购买成功!")
# 改签函数
def change_ticket(user):
order_id = int(input("请输入要改签的订单号:"))
if order_id < 0 or order_id >= len(user['orders']):
print("输入错误!")
return
order = user['orders'][order_id]
new_train_id = input("请输入新的车次:")
if new_train_id not in trains:
print("输入错误!")
return
new_train = trains[new_train_id]
if new_train['start'] != order['start'] or new_train['end'] != order['end']:
print("新车次无法到达目的地!")
return
if new_train['seats'] == 0:
print("新车次无余票!")
return
if input("确认改签?(y/n)") != 'y':
print("取消改签!")
return
order['train_id'] = new_train_id
order['price'] = new_train['price']
trains[new_train_id]['seats'] -= 1
trains[order['train_id']]['seats'] += 1
print("改签成功!")
# 退票函数
def refund_ticket(user):
order_id = int(input("请输入要退票的订单号:"))
if order_id < 0 or order_id >= len(user['orders']):
print("输入错误!")
return
order = user['orders'][order_id]
if input("确认退票?(y/n)") != 'y':
print("取消退票!")
return
user['orders'].pop(order_id)
trains[order['train_id']]['seats'] += 1
print("退票成功!")
# 主程序
while True:
print("欢迎使用火车售票系统!")
print("1. 注册 2. 浏览 3. 购买 4. 改签 5. 退票 6. 退出")
choice = input("请选择操作:")
if choice == '1':
register()
elif choice == '2':
show_trains()
elif choice == '3':
id_card = input("请输入身份证号:")
user = None
for u in users:
if u['id_card'] == id_card:
user = u
break
if user is None:
print("用户不存在!")
continue
buy_ticket(user)
elif choice == '4':
id_card = input("请输入身份证号:")
user = None
for u in users:
if u['id_card'] == id_card:
user = u
break
if user is None:
print("用户不存在!")
continue
change_ticket(user)
elif choice == '5':
id_card = input("请输入身份证号:")
user = None
for u in users:
if u['id_card'] == id_card:
user = u
break
if user is None:
print("用户不存在!")
continue
refund_ticket(user)
elif choice == '6':
print("谢谢使用!")
break
else:
print("输入错误!")
```
这个火车售票系统是用 Python 实现的,采用了字典来存储车辆信息和用户信息,实现了注册、浏览、购买、改签和退票等功能,可以显示多个车次每个车次要有不同的座位,不同的价格,可以一次性购买多张票,有余票限制,车次时间,和起点站,终点站,购买完成之后会显示订单。
阅读全文