python基础练习-多字典嵌套三级菜单

# 不输入不会循环, 如果输入为空回车,continue跳出本次循环并重新进入本次循环 所以会继续打印菜单第一级.因为输入回车就等于开始执行下一级,
# 所以一般回车之后进入新的循环,展示新的循环.注意每一个输入分别输入q时候退出哪次对应的循环.写到最后输入的判断结束,不需要再打印



menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '网易':{},
                'google':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{},
            },
            '上地':{
                '百度':{},
            },
        },
        '昌平':{
            '沙河':{
                
                '北航':{},
            },
            '天通苑':{},
            '回龙观':{},
        },
        '朝阳':{},
        '东城':{},
    },
    '上海':{
        '闵行':{
            "人民广场":{
                '炸鸡店':{}
            }
        },
        '闸北':{
            '火车战':{
                '携程':{}
            }
        },
        '浦东':{},
    },
    '山东':{},
}


menu_flag=False


while not menu_flag:


    for key in menu:
        print(key)

    choice=input(">:").strip()

    if len(choice) == 0: continue
    if choice == 'b': break
    if choice == 'q':
        menu_flag = True
    if choice in menu:
        menu2 = menu[choice]
        for key in menu2:
            print(key)

        choice1=input(">>:").strip()
        while not menu_flag:
            if len(choice1)==0:continue
            if choice1=='b':break
            if choice1=='q':menu_flag=True
            if choice1 in menu2:
                menu3=menu2[choice1]
                for key in menu3:
                    print(key)

                choice2=input(">>>:").strip()
                while not menu_flag:
                    if len(choice2)==0:continue
                    if choice2=='b':break
                    if choice2=='q':menu_flag=True
                    if choice2 in menu3:
                        menu4=menu3[choice2]
                        for key in menu4:
                            print(key)
                        choice3=input(">>>>:").strip()
                        while not menu_flag:
                            if len(choice3)==0:continue
                            if choice3=='b':break
                            if choice3=='q':menu_flag=True

  

 

 

优化版

menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '网易':{},
                'google':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{},
            },
            '上地':{
                '百度':{},
            },
        },
        '昌平':{
            '沙河':{
                '高教园':{},
                '北航':{},
            },
            '天通苑':{},
            '回龙观':{},
        },
        '朝阳':{},
        '东城':{},
    },
    '上海':{
        '闵行':{
            "人民广场":{
                '炸鸡店':{}
            }
        },
        '闸北':{
            '火车站':{
                '携程':{}
            }
        },
        '浦东':{},
    },
    '山东':{},
}





exit_flag=False

current_layer=menu

layers=[menu]

while not exit_flag:
    for key in current_layer:
       print(key)

    choice=input(">>:").strip()

    if len(choice)==0:continue

    elif choice=='b':
        current_layer=layers[-2]
        layers.pop()
    elif choice=='q':
        exit_flag=True
        break
    elif choice not in current_layer:
        print("没有下一级!输入b返回上一级,输入q结束!")
        continue
    else:
        layers.append(current_layer)
        current_layer=current_layer[choice]

  

posted @ 2017-09-07 19:42  Adamanter  阅读(161)  评论(0编辑  收藏  举报