python之三级目录

#python之三级目录低配版

menu = {
'北京':{
        '朝阳':{
            '国贸':{
                'CICC':{ },
                'HP':{ },
                '渣打银行':{ },
                'CCTV':{ },
            },
            '望京':{
                '陌陌':{ },
                 '奔驰':{ },
                 '360':{ },
            },
            '三里屯':{
                '优衣库':{ },
                'apple':{ },
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{ },
                '阿泰包子':{ },
            },
            '天通苑':{
                '链家':{ },
                '我爱我家':{ },
            },
            '回龙观':{  },
        },
        '海淀':{
            '五道口':{
                '谷歌':{ },
                '网易':{ },
                '搜狐':{ },
                'sogo':{ },
                '快手':{ },
            },
            '中关村':{
                'youku':{ },
                'iqiyi':{ },
                '汽车之家':{ },
                '新东方':{ },
                'QQ':{ },
            }
        },
    },
'上海':{
    '浦东':{
        '陆家嘴':{
            'CICC':{ },
             '高盛':{ },
            '摩根':{ },
        },
        '外滩':{ },

    },
    '闵行':{ },
    '静安':{ },
},
'山东':{
    '济南':{

    },
    '德州':{
        '乐陵':{
            '丁坞镇':{ },
            '城区':{ },
        },
        '平原':{ },
    },
    '青岛':{ },
},
}



back_flag=False
exit_flag=False
while not back_flag and not exit_flag:
    for key in menu:
        print(key)
    choice=input("1>>:").strip()
    if choice=="q":
        exit_flag=True
    if choice in menu:
        while not back_flag and not exit_flag:
            for key2 in menu[choice]:
                print(key2)
            choice2=input("2>>:").strip()
            if choice2 =='q':
                exit_flag=True
            if choice2 =='b':
                back_flag=True
            if choice2 in menu[choice]:
                while not back_flag and not exit_flag:
                    for key3 in menu[choice][choice2]:
                        print(key3)
                    choice3=input("3>>:").strip()
                    if choice3=='q':
                        exit_flag=True
                    if choice3=='b':
                        back_flag=True
                    if choice3 in menu[choice][choice2]:
                        while not back_flag and not exit_flag:
                            for key4 in menu[choice][choice2][choice3]:
                                print(key4)
                            choice4=input("4>>:").strip()
                            print('last level')
                            if choice4=='q':
                                exit_flag=True
                            if choice4=='b':
                                back_flag=True
                        else:
                            back_flag=False
                else:
                    back_flag=False
        else:
            back_flag=False
#python之三级目录高级版
menu = {
'北京':{
        '朝阳':{
            '国贸':{
                'CICC':{ },
                'HP':{ },
                '渣打银行':{ },
                'CCTV':{ }
            },
            '望京':{
                '陌陌':{ },
                 '奔驰':{ },
                 '360':{ }
            },
            '三里屯':{
                '优衣库':{ },
                'apple':{ }
            }
        },
        '昌平':{
            '沙河':{
                '老男孩':{ },
                '阿泰包子':{ }
            },
            '天通苑':{
                '链家':{ },
                '我爱我家':{ }
            },
            '回龙观':{  },
        },
        '海淀':{
            '五道口':{
                '谷歌':{ },
                '网易':{ },
                '搜狐':{ },
                'sogo':{ },
                '快手':{ }
            },
            '中关村':{
                'youku':{ },
                'iqiyi':{ },
                '汽车之家':{ },
                '新东方':{ },
                'QQ':{ }
            }
        }
    },
'上海':{
    '浦东':{
        '陆家嘴':{
            'CICC':{ },
             '高盛':{ },
            '摩根':{ }
        },
        '外滩':{ }

    },
    '闵行':{ },
    '静安':{ }
},
'山东':{
    '济南':{

    },
    '德州':{
        '乐陵':{
            '丁坞镇':{ },
            '城区':{ }
        },
        '平原':{ }
    },
    '青岛':{ }
}
}
current_layer = menu
parent_layers=[ ]   #保存所有父级,最后一个元素永远是父亲
while True:
    for key in current_layer:
        print(key)
    choice=input(">>>:").strip()
    if len(choice) == 0: continue
    if choice in current_layer:
       # parent_layer=current_layer  #改之前相当于护父亲
       parent_layers.append(current_layer)
       current_layer=current_layer[choice] #改成子层
    elif choice == "b":
        if parent_layers:
            current_layer=parent_layers.pop()#弹出列表最后一个元素,并返回该元素的值
    else:
        print("无此项")

猜你喜欢

转载自www.cnblogs.com/wangxy629/p/10250148.html