python -day8

2022/2/7 17:13:18

本文主要是介绍python -day8,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

2、字典:

  增删改查

    a={1:'a',2:'b',3:'c'}

    b={4:'d'}

    1)增:a.update(b)    

        a         >>>{1:'a',2:'b',3:'c',4:'d'}

    2)删:a.pop(3)

        a  >>>{1:'a',2:'b'}

    3)改:a[3]='aaa'

        a  >>>{1:'a',2:'b',3:'aaa'}

    4)清空 a.clear()

        a  >>>{}

 

 

 

task1  编写一段车站订票程序,要求实现:查询车次表,查询已订票信息,订票,退票及改签等功能

 

 

train_time_list = [
    {
        "车次": "D4605",
        "始发站": "北京",
        "到达站": "长春",
        "出发时间": "02:56",
        "到达时间": "09:16",
        "历时": "06:20"
    },
    {
        "车次": "D29",
        "始发站": "北京",
        "到达站": "长春西",
        "出发时间": "06:34",
        "到达时间": "13:13",
        "历时": "06:39"
    },
    {
        "车次": "D21",
        "始发站": "北京",
        "到达站": "长春",
        "出发时间": "06:39",
        "到达时间": "13:05",
        "历时": "07:26"
    },
    {
        "车次": "D25",
        "始发站": "北京",
        "到达站": "长春西",
        "出发时间": "10:28",
        "到达时间": "17:26",
        "历时": "06:58"
    },
    {
        "车次": "D101",
        "始发站": "北京",
        "到达站": "长春西",
        "出发时间": "13:52",
        "到达时间": "20:27",
        "历时": "06:35"
    },
    {
        "车次": "D19",
        "始发站": "北京",
        "到达站": "长春",
        "出发时间": "14:18",
        "到达时间": "20:45",
        "历时": "06:27"
    },
    {
        "车次": "D73",
        "始发站": "北京",
        "到达站": "长春",
        "出发时间": "14:28",
        "到达时间": "20:51",
        "历时": "06:23"
    },
    {
        "车次": "D23",
        "始发站": "北京",
        "到达站": "长春西",
        "出发时间": "15:33",
        "到达时间": "22:10",
        "历时": "06:37"
    },
    {
        "车次": "D4609",
        "始发站": "北京",
        "到达站": "长春西",
        "出发时间": "23:52",
        "到达时间": "07:04",
        "历时": "07:12"
    }
]

# 申明用户购票信息
user_ticket ={}
name = input('请输入您的姓名来登录系统')
# 系统提示语
print('尊敬的用户您好,欢迎登录订票系统,请输入您想办理的业务:')
while True:
    user_do= input('查询列车时刻表请按0;查询已订票业务请按1;订票请按2,退票请按3,改签请按4,退出请按*')
    # 查询列车时刻表
    if user_do == '0':
        print("%-10s%-10s%-10s%-10s%-10s%-10s" % ("车次", "始发站", "到达站", "出发时间", "到达时间", "历时"))
        for i in range(len(train_time_list)):
            print("%-10s%-10s%-10s%-10s%-10s%-10s" % (train_time_list[i].get("车次"),train_time_list[i].get("始发站"),train_time_list[i].get("到达站"),train_time_list[i].get("出发时间"),train_time_list[i].get("到达时间"),train_time_list[i].get("历时")))


    # 查询已订票情况
    if user_do =='1':
       if user_ticket !={}:
           print('您好,这边查询到您订的车票信息:姓名{}的车次号为{},始发站为{},到达站为{},出发时间为{},到达时间为{},历时{}'.format(
                                                                                         user_ticket.get("姓名"),
                                                                                         user_ticket.get("车次"),
                                                                                         user_ticket.get("始发站"),
                                                                                         user_ticket.get("到达站"),
                                                                                         user_ticket.get("出发时间"),
                                                                                         user_ticket.get("到达时间"),
                                                                                         user_ticket.get("历时")))
       else:
           print('抱歉,没有查询到您的购票信息')
    # 订票流程
    if user_do =='2':
        #name = input('请输入您的姓名')
        user_book_ticket =[]
        # 检查车次号是否在列表内
        while True:
            user_tran_no = input('请选择车次号')
            for i in range(len(train_time_list)):
                user_book_ticket.append(train_time_list[i].get('车次'))
            if user_tran_no not in user_book_ticket:
                print('此车号不存在,请重新输入')
            else:
                # 遍历列车表找到车次号所在
                for m in range(len(train_time_list)):
                    if train_time_list[m].get('车次') == user_tran_no:
                        user_ticket['姓名']= name
                        user_ticket['车次'] = train_time_list[m].get('车次')
                        user_ticket['始发站'] = train_time_list[m].get('始发站')
                        user_ticket['到达站'] = train_time_list[m].get('到达站')
                        user_ticket['出发时间'] = train_time_list[m].get('出发时间')
                        user_ticket['到达时间'] = train_time_list[m].get('到达时间')
                        user_ticket['历时'] = train_time_list[m].get('历时')
                print('您好,您订的车票信息:姓名{}的车次号为{},始发站为{},到达站为{},出发时间为{},到达时间为{},历时{}'.format(
                                                                                                  user_ticket.get("姓名"),
                                                                                                  user_ticket.get("车次"),
                                                                                                  user_ticket.get("始发站"),
                                                                                                  user_ticket.get("到达站"),
                                                                                                  user_ticket.get("出发时间"),
                                                                                                  user_ticket.get("到达时间"),
                                                                                                  user_ticket.get("历时")))
            break
# 选择退票
    if user_do =='3':
        # 判断是否有票
        #name = input('请输入您的姓名')
        if user_ticket =={} or name !=user_ticket.get('姓名'):
            print('未查询到您的车票')
        else:
            print('您好,这边查询到您订的车票信息:姓名{}的车次号为{},始发站为{},到达站为{},出发时间为{},到达时间为{},历时{}'.format(
                                                                                             user_ticket.get("姓名"),
                                                                                             user_ticket.get("车次"),
                                                                                             user_ticket.get("始发站"),
                                                                                             user_ticket.get("到达站"),
                                                                                             user_ticket.get("出发时间"),
                                                                                             user_ticket.get("到达时间"),
                                                                                             user_ticket.get("历时")))
        user_return_ticket = input('请问是否退票,请回复Y or N')
        if user_return_ticket =='N':
            continue
        else:
            user_ticket={}
            print('已成功退票')

    if user_do =='4':
        #name = input('请输入您的名字')
        if user_ticket =={} or name !=user_ticket.get('姓名') :
            print('未查询到车票')
        else:
            print('您好,这边查询到您订的车票信息:姓名{}的车次号为{},始发站为{},到达站为{},出发时间为{},到达时间为{},历时{}'.format(
                                                                                                         user_ticket.get("姓名"),
                                                                                                         user_ticket.get("车次"),
                                                                                                         user_ticket.get("始发站"),
                                                                                                         user_ticket.get("到达站"),
                                                                                                         user_ticket.get("出发时间"),
                                                                                                         user_ticket.get("到达时间"),
                                                                                                         user_ticket.get("历时")))
            user_rebook = input('是否确认改签,请输入Y or N')
            if user_rebook =='N':
                continue
            else:
               #  改签车票的信息
                user_rebook_ticket = []
                while True:
                # 改签的车次
                    user_tran_rebook_no = input('请输入的改签的车次号')
                # 获取车次信息
                    for n in range(len(train_time_list)):
                        user_rebook_ticket.append(train_time_list[n].get('车次'))
                # 判断车次是否存在
                    if user_tran_rebook_no not in user_rebook_ticket:
                        print('无此车次')
                        continue
            # 遍历车号
                    else:
                        for x in range(len(train_time_list)):
                            if train_time_list[x].get('车次') == user_tran_rebook_no:
                                user_ticket['车次']= train_time_list[x].get('车次')
                                user_ticket['姓名']=name
                                user_ticket['始发站']= train_time_list[x].get('始发站')
                                user_ticket['到达站'] = train_time_list[x].get('到达站')
                                user_ticket['出发时间']= train_time_list[x].get('出发时间')
                                user_ticket['到达时间'] = train_time_list[x].get('到达时间')
                                user_ticket['历时'] = train_time_list[x].get('历时')
                        print('您的车票已改签,新的车票内容如下:姓名{}的车次号为{},始发站为{},到达站为{},出发时间为{},到达时间为{},历时{}'.format(
                                                                                                     user_ticket.get("姓名"),
                                                                                                     user_ticket.get("车次"),
                                                                                                     user_ticket.get("始发站"),
                                                                                                     user_ticket.get("到达站"),
                                                                                                     user_ticket.get("出发时间"),
                                                                                                     user_ticket.get("到达时间"),
                                                                                                     user_ticket.get("历时")))
                    break


    if user_do == '*':
        print('退出程序')
        break


    out = input('返回上级请按#,退出程序请按*')
    if out == '#':
        continue
    elif out == '*':
        break

 

    



这篇关于python -day8的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程