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('\033[31m%s \033[0m' % '请输入序号'.ljust(20, '*'))
    print('\033[31m***输入back返回上一级菜单,或者quit退出***\033[0m')

    for i in current_layer:
        print(i)

    choose = input('>>>:').strip()

    if choose in current_layer:
        if choose in choose_end:
            print('\033[31m%s \033[0m' % '已经到最后一页'.center(40, '*'))
            continue
        else:
            parent_layer.append(current_layer)
            current_layer = current_layer[choose]
        # else:
        #     print('\033[31m%s \033[0m' % '已经到最后一页'.center(40, '*'))

    elif choose == 'back':
        if len(parent_layer) != 0:
            current_layer = parent_layer.pop()
        else:
            print('\033[31m%s \033[0m' % '已经到达最上级菜单'.center(40, '*'))

    elif choose == 'quit':
        print('\033[31m Bye~Bye~^_^ \033[0m')
        break

    else:
        print('\033[31m%s \033[0m' % '输入有误,请重新输入'.center(40, '*'))

 

posted @ 2018-03-28 20:23  时光飞逝,逝者如斯  阅读(450)  评论(0编辑  收藏  举报