zoukankan      html  css  js  c++  java
  • Python实现简单的三级菜单

    话不多说,直奔代码

    # 要处理的字典
    dic1 = {
        '北京': {
            '东城':
                {
                    '沙河': ['沙河机场', '链家'],
                    '天通苑': ['北方明珠', '天通尾货']
                },
            '朝阳':
                {
                    '花家地': ['朝阳公园', '望京soho'],
                    '北小河': ['北小河公园', '北京中学']
                }
        },
        '上海': {
            '虹桥':
                {
                    '虹桥机场': ['超市', '特产店', '水吧'],
                    '东方明珠': ['电影院', '游泳馆', '餐馆']
                },
            '浦东':
                {
                    '景秀路': ['世纪公园', '立交桥'],
                    '中环路': ['鲁迅公园', '同济大学']
                }
        },
        '河北': {
            '石家庄':
                {
                    '行唐': ['东正', '阳关'],
                    '赵县': ['赵州桥', '高村乡']
                },
            '唐山':
                {
                    '滦南县': ['司各庄镇', '安各庄镇'],
                    '玉田县': ['玉田镇', '亮甲店镇']
                }
        }
    }
    # 方法一:
    tag=True
    while tag:
        menu1=menu
        for key in menu1:
            print(key)
    
        choice1=input('The First Layer>>: ').strip()
        if choice1 == 'b':break
        if choice1 == 'q':
            tag = False
            break
        if choice1 not in menu1:continue
    
        while tag:
            menu2=menu1[choice1] 
            for key in menu2:
                print(key)
    
            choice2 = input('The Second Layer>>: ').strip()
            if choice2 == 'b':break
            if choice2 == 'q':
                tag=False
                break
            if choice2 not in menu2:continue
    
            while tag:
                menu3 = menu2[choice2]             for key in menu3:
                    print(key)
    
                choice3 = input('The Third Layer>>: ').strip() 
                if choice3 == 'b':break
                if choice3 == 'q':
                    tag=False
                    break
                if choice3 not in menu3: continue
    
                while tag:
                    menu4 = menu3[choice3]                  for key in menu4:
                        print(key)
    
                    choice4 = input('The Fourth Layer>>: ').strip()
                    if choice4 == 'b':break
                    if choice4 == 'q':
                        tag=False
                        break
    
                    if choice4 not in menu4: continue
    current_layer = dic1
    parent_layer = []  # 用来放置父级的key组成的list
    choose_end=['沙河机场', '链家','北方明珠', '天通尾货','朝阳公园', '望京soho','北小河公园', '北京中学','超市', '特产店', '水吧',
                '电影院', '游泳馆', '餐馆','世纪公园', '立交桥','鲁迅公园', '同济大学','东正', '阳关','赵州桥', '高村乡','司各庄镇',
                '安各庄镇','玉田镇', '亮甲店镇',
                ]
    
    Tag = True
    while Tag:
    
        print('33[31m%s 33[0m' % '请输入序号'.ljust(20, '*'))
        print('33[31m***输入back返回上一级菜单,或者quit退出***33[0m')
    
        for i in current_layer:
            print(i)
    
        choose = input('>>>:').strip()
    
        if choose in current_layer:
            if choose in choose_end:
                print('33[31m%s 33[0m' % '已经到最后一页'.center(40, '*'))
                continue
            else:
                parent_layer.append(current_layer)
                current_layer = current_layer[choose]
            # else:
            #     print('33[31m%s 33[0m' % '已经到最后一页'.center(40, '*'))
    
        elif choose == 'back':
            if len(parent_layer) != 0:
                current_layer = parent_layer.pop()
            else:
                print('33[31m%s 33[0m' % '已经到达最上级菜单'.center(40, '*'))
    
        elif choose == 'quit':
            print('33[31m Bye~Bye~^_^ 33[0m')
            break
    
        else:
            print('33[31m%s 33[0m' % '输入有误,请重新输入'.center(40, '*'))
  • 相关阅读:
    【算法】数据结构与算法基础总览(上)数据结构篇
    Hangfire只允许同时运行同一个任务
    Redis缓存系列--(六)缓存和数据库一致性更新原则
    Redis缓存系列--(五)自定义Redis缓存注解的使用
    Redis缓存系列--(四)Redis基础数据类型在Java中的使用
    Redis缓存系列--(三)redis内存管理
    Redis缓存系列--(二)Redis持久化机制
    Redis缓存系列--(一)Redis的编译安装以及服务的开启
    深入理解Java虚拟机--垃圾收集器与内存分配策略
    分布式系统系列--(四)LVS基础知识点介绍
  • 原文地址:https://www.cnblogs.com/JetpropelledSnake/p/8648439.html
Copyright © 2011-2022 走看看