完成地铁简易购票系统的Python代码 1.成人票5.0元一张 儿童票2.5元一张 2.输入购买成人票数量 输入购买儿童票数量 3.显示需要支付的金额
时间: 2023-04-02 08:00:45 浏览: 64
以下是地铁简易购票系统的 Python 代码,可以实现输入购买成人票数量和儿童票数量,计算需要支付的金额:
adult_price = 5.
child_price = 2.5
adult_num = int(input("请输入购买成人票数量:"))
child_num = int(input("请输入购买儿童票数量:"))
total_price = adult_num * adult_price + child_num * child_price
print("需要支付的金额为:", total_price, "元")
相关问题
用python写一个火车票购买系统,有成人与儿童票,可以查询余票和退票等
好的,下面是一个简单的实现示例:
```python
import random
# 火车票信息
tickets = [
{"train_no": "G1234", "from_station": "北京", "to_station": "上海", "start_time": "08:00", "end_time": "13:00", "price": 500, "adult_num": 100, "child_num": 50},
{"train_no": "G2345", "from_station": "上海", "to_station": "北京", "start_time": "14:00", "end_time": "19:00", "price": 500, "adult_num": 80, "child_num": 30},
{"train_no": "G3456", "from_station": "北京", "to_station": "广州", "start_time": "09:00", "end_time": "18:00", "price": 800, "adult_num": 120, "child_num": 60},
{"train_no": "G4567", "from_station": "广州", "to_station": "北京", "start_time": "10:00", "end_time": "19:00", "price": 800, "adult_num": 100, "child_num": 40},
]
# 用户信息
users = [
{"name": "张三", "id_card": "110101199001010001", "order_history": []},
{"name": "李四", "id_card": "110101199101010001", "order_history": []},
]
# 订单号
order_id = 1
# 查询余票
def query_tickets(from_station, to_station, date):
for ticket in tickets:
if ticket["from_station"] == from_station and ticket["to_station"] == to_station and ticket["start_time"].startswith(date):
print("车次:{},出发时间:{},到达时间:{},价格:{},成人票余票:{},儿童票余票:{}".format(ticket["train_no"], ticket["start_time"], ticket["end_time"], ticket["price"], ticket["adult_num"], ticket["child_num"]))
# 购买车票
def buy_ticket(train_no, from_station, to_station, date, adult_num, child_num, name, id_card):
global order_id
for ticket in tickets:
if ticket["train_no"] == train_no and ticket["from_station"] == from_station and ticket["to_station"] == to_station and ticket["start_time"].startswith(date):
if ticket["adult_num"] >= adult_num and ticket["child_num"] >= child_num:
ticket["adult_num"] -= adult_num
ticket["child_num"] -= child_num
order = {"order_id": order_id, "name": name, "id_card": id_card, "train_no": train_no, "from_station": from_station, "to_station": to_station, "date": date, "adult_num": adult_num, "child_num": child_num, "price": ticket["price"] * adult_num}
order_id += 1
for user in users:
if user["name"] == name and user["id_card"] == id_card:
user["order_history"].append(order)
break
else:
user = {"name": name, "id_card": id_card, "order_history": [order]}
users.append(user)
print("购票成功,订单号为:{}".format(order["order_id"]))
else:
print("余票不足,购票失败")
# 退票
def refund_ticket(order_id, name, id_card):
for user in users:
if user["name"] == name and user["id_card"] == id_card:
for order in user["order_history"]:
if order["order_id"] == order_id:
for ticket in tickets:
if ticket["train_no"] == order["train_no"] and ticket["from_station"] == order["from_station"] and ticket["to_station"] == order["to_station"] and ticket["start_time"].startswith(order["date"]):
ticket["adult_num"] += order["adult_num"]
ticket["child_num"] += order["child_num"]
user["order_history"].remove(order)
print("退票成功")
return
else:
print("订单不存在")
return
else:
print("用户不存在")
# 查询订单
def query_order(order_id=None, name=None, id_card=None):
if order_id:
for user in users:
for order in user["order_history"]:
if order["order_id"] == order_id:
print("订单号:{},姓名:{},身份证号:{},车次:{},出发地:{},目的地:{},日期:{},成人票数量:{},儿童票数量:{},价格:{}".format(order["order_id"], order["name"], order["id_card"], order["train_no"], order["from_station"], order["to_station"], order["date"], order["adult_num"], order["child_num"], order["price"]))
return
else:
print("订单不存在")
elif name and id_card:
for user in users:
if user["name"] == name and user["id_card"] == id_card:
for order in user["order_history"]:
print("订单号:{},姓名:{},身份证号:{},车次:{},出发地:{},目的地:{},日期:{},成人票数量:{},儿童票数量:{},价格:{}".format(order["order_id"], order["name"], order["id_card"], order["train_no"], order["from_station"], order["to_station"], order["date"], order["adult_num"], order["child_num"], order["price"]))
else:
print("该用户没有购买火车票")
else:
print("用户不存在")
# 测试
if __name__ == "__main__":
# 查询余票
query_tickets("北京", "上海", "2022-01-01")
# 购买车票
buy_ticket("G1234", "北京", "上海", "2022-01-01", 2, 1, "张三", "110101199001010001")
# 退票
refund_ticket(1, "张三", "110101199001010001")
# 查询订单
query_order(1)
query_order(name="张三", id_card="110101199001010001")
```
这个示例实现了查询余票、购买车票、退票、查询订单等功能。你可以根据需求进行修改和扩展。
用Python写一个简易的高铁购票系统,有登录系统,有几等座,成人票和儿童票,显示票数,退票
好的,我可以为你提供这个高铁购票系统的基本代码框架,你可以在此基础上进行完善和修改。以下是代码:
```python
# 定义票价和座位等级
price = {'一等座': 100, '二等座': 80}
level = ['一等座', '二等座']
# 定义车票信息
ticket_info = {
'G1001': {'from': '北京', 'to': '上海', 'date': '2022-01-01', '一等座': 20, '二等座': 50},
'G1002': {'from': '北京', 'to': '广州', 'date': '2022-01-02', '一等座': 30, '二等座': 60},
'G1003': {'from': '上海', 'to': '广州', 'date': '2022-01-03', '一等座': 10, '二等座': 40},
}
# 定义用户信息
user_info = {
'user1': {'password': '123456', 'name': '张三', 'tickets': {}},
'user2': {'password': 'abcdef', 'name': '李四', 'tickets': {}},
}
# 登录系统
def login():
username = input('请输入用户名:')
password = input('请输入密码:')
if username in user_info and user_info[username]['password'] == password:
print('登录成功')
return username
else:
print('用户名或密码错误')
return None
# 查询车票
def query_ticket():
from_station = input('请输入出发站:')
to_station = input('请输入到达站:')
date = input('请输入出发日期(格式为YYYY-MM-DD):')
print('车次\t出发站\t到达站\t出发时间\t到达时间\t一等座\t二等座')
for k, v in ticket_info.items():
if v['from'] == from_station and v['to'] == to_station and v['date'] == date:
print(f'{k}\t{v["from"]}\t{v["to"]}\t{v["date"]} 08:00\t{v["date"]} 12:00\t{v["一等座"]}\t{v["二等座"]}')
# 购买车票
def buy_ticket(username):
print('请选择要购买的车次和座位等级:')
for k, v in ticket_info.items():
print(f'{k} {v["from"]} - {v["to"]} {v["date"]} 一等座:{v["一等座"]} 二等座:{v["二等座"]}')
train_no = input('请输入车次:')
if train_no not in ticket_info:
print('车次不存在')
return
seat_level = input('请输入座位等级(一等座/二等座):')
if seat_level not in level:
print('座位等级输入错误')
return
num = int(input('请输入购买数量:'))
if num <= 0:
print('购买数量输入错误')
return
if ticket_info[train_no][seat_level] < num:
print('余票不足')
return
total_price = price[seat_level] * num
print(f'总价为:{total_price}元')
confirm = input('是否确认购买(Y/N):')
if confirm == 'Y':
# 减少余票数量
ticket_info[train_no][seat_level] -= num
# 记录购票信息
user_info[username]['tickets'][train_no] = user_info[username]['tickets'].get(train_no, {level[0]: 0, level[1]: 0})
user_info[username]['tickets'][train_no][seat_level] += num
print('购买成功')
else:
print('已取消购买')
# 查询购票信息
def query_order(username):
print('您的购票信息如下:')
for k, v in user_info[username]['tickets'].items():
print(f'车次:{k} 一等座:{v[level[0]]} 二等座:{v[level[1]]}')
# 退票
def refund_ticket(username):
print('请选择要退票的车次和座位等级:')
for k, v in user_info[username]['tickets'].items():
print(f'车次{k} 一等座:{v[level[0]]} 二等座:{v[level[1]]}')
train_no = input('请输入车次:')
if train_no not in user_info[username]['tickets']:
print('车次不存在')
return
seat_level = input('请输入座位等级(一等座/二等座):')
if seat_level not in level:
print('座位等级输入错误')
return
num = int(input('请输入退票数量:'))
if num <= 0 or num > user_info[username]['tickets'][train_no][seat_level]:
print('退票数量输入错误')
return
refund_price = price[seat_level] * num
print(f'退票金额为:{refund_price}元')
confirm = input('是否确认退票(Y/N):')
if confirm == 'Y':
# 增加余票数量
ticket_info[train_no][seat_level] += num
# 更新购票信息
user_info[username]['tickets'][train_no][seat_level] -= num
print('退票成功')
else:
print('已取消退票')
# 主函数
def main():
username = login()
if not username:
return
while True:
print('请选择您要进行的操作:')
print('1. 查询车票')
print('2. 购买车票')
print('3. 查询购票信息')
print('4. 退票')
print('5. 退出系统')
choice = input()
if choice == '1':
query_ticket()
elif choice == '2':
buy_ticket(username)
elif choice == '3':
query_order(username)
elif choice == '4':
refund_ticket(username)
elif choice == '5':
break
else:
print('输入错误,请重新输入')
if __name__ == '__main__':
main()
```
这个系统实现了登录、查询车票、购买车票、查询购票信息、退票这几个功能,你可以根据自己的需要进行修改和完善。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)