python练习题:三级菜单

需求:
可依次选择进入各子菜单
可从任意一层往回退到上一层
可从任意一层退出程序
所需新知识点:列表、字典

测试环境:win7系统,python3.7.0,工具:pycharm-community-2018.1.4

 
menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '网易':{},
                'google':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{},
            },
            '上地':{
                '百度':{},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{},
            },
            '天通苑':{},
            '回龙观':{},
        },
        '朝阳':{},
        '东城':{},
    },
    '上海':{
        '闵行':{
            "人民广场":{
                '炸鸡店':{}
            }
        },
        '闸北':{
            '火车站':{
                '携程':{}
            }
        },
        '浦东':{},
    },
    '山东':{},
}
# 需求:
# 可依次选择进入各子菜单
# 可从任意一层往回退到上一层
# 可从任意一层退出程序
# 所需新知识点:列表、字典
import sys
while True:
    for i in menu:
        print('	', i)
    choice = input("以上为 一级菜单 请选择(q退出,a返回上一层,若输入有误继续执行输入)>:")
    if choice == 'q':
         sys.exit(0)
    elif choice == 'a':
        print('	','已经是第一层了')
    # 判断输入是否在一级目录里面
    if choice in menu:

        while True:
            # 输出一级目录对应的二级目录
            for i2 in menu[choice]:
                print('	',i2)
            choice2 = input("以上为 二级菜单 请选择(q退出,a返回上一层,若输入有误继续执行输入)>:")
            if choice2 == 'q':
                 sys.exit(0)
            if choice2 == 'a':
                break
            #判断 输入 是否在二级目录里面
            if choice2 in menu[choice]:

                while True:
                    # 输出二级目录对应的三级目录
                    for i3 in menu[choice][choice2]:
                        print('	',i3)
                    choice3 =  input("以上为 三级菜单 请选择(q退出,a返回上一层,若输入有误继续执行输入)>:")
                    if choice3 == 'q':
                         sys.exit(0)
                    if choice3 == 'a':
                        break
                    # 判断输入是否在 三级目录里面
                    if choice3 in menu[choice][choice2]:

                        while True:
                            # 输出三级目录下对应的四级目录
                            for i4 in menu[choice][choice2][choice3]:
                                print('	',i4)
                            choice4 = input("以上为 最后一层 请选择(q退出,a返回上一层,若输入有误继续执行输入)>:")
                            if choice4 == 'q':
                                 sys.exit(0)
                            if choice4 == 'a':
                                break

 

#--------------------------代码简化版------------------------------------
# 发现有重复内容,不要写重复的代码,
current_layer = menu
last_layer = []
while True:

    for i in current_layer:
        print(i)
    choice = input('>').strip()
    if choice  in current_layer:
        last_layer.append(current_layer)
        print(last_layer)
        current_layer = current_layer[choice]

    elif choice == 'q':
            break
    elif choice == 'a':
        if len(last_layer) != 0:
            last = last_layer.pop()
            current_layer = last
            print(type(current_layer))
        else:
            print('已经是第一层了')
    else:
        print('输入错误,请重新输入:')
        continue


 # print("删除第一个元素:",sentence.pop(0)  、pop() 函数用于移除列表中的指定索引位置的一个元素(默认最后一个元素),并且返回该元素的值。

原文地址:https://www.cnblogs.com/foremostxl/p/9432910.html