进入三级目录习题讲解

1 先看菜单定义的形式, 用字典形式逐渐递归

menu ={}

menu = {"1":{},"2":{},"3":{}}

menu = {"1":

       {"11":{},"12":{},"13":{}}

    ,"2":{}

    ,"3":{}}

就是每个value的格式和menu的格式是一样的

2 先打印一个列表

    for i in menu:
        print(i)
    chose = input(">>")

3 继续往下走

    if chose in menu:
        menu = menu[chose]

4 要使整个能够循环起来

while True:
    for i in menu:
        print(i)
    chose = input(">>")

    if chose in menu:
        menu = menu[chose]

5 考虑返回问题

  要实现返回的功能我提供的是 通过另一个 变量来记录之前访问过的路径

list = []

while True:
    for i in menu:
        print(i)
    chose = input(">>")

    if chose in menu:
        list.append(menu)
        menu = menu[chose]

6 实现返回

map = menu
list = []

while True:
    for i in menu:
        print(i)
    chose = input(">>")

    if chose in menu:
        list.append(map)
        menu = menu[chose]
    if chose == 'b':
        if list == []:
            map = menu
        else:
            map = list[len(list)-1]
            list.pop()

7 实现退出

map = menu
history = []

while True:
    for i in menu:
        print(i)
    chose = input(">>")

    if chose in menu:
        list.append(map)
        menu = menu[chose]
    if chose == 'b':
        if list == []:
            map = menu
        else:
            map = list[len(list)-1]
            list.pop()
    if chose == 'q':
        print("再见!")
        break

8 修复两个问题

map = menu
list = []

while True:
    for i in map:
        print(i)
    chose = input("请输入查询的地区名字:")
    if chose in map:
        if len(map[chose])==0:
            print("没有内容了, 请返回上一层..")
        list.append(map)
        map = map[chose]
    if chose == 'b':
        if list == []:
            map = menu
        else:
            map = list[len(list)-1]
            list.pop()
    if chose == 'q':
        print("再见!")
        break

9 整个程序 

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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

print("=====欢迎查询地区地图=====")

map = menu
list = []

while True:
    for i in map:
        print(i)
    chose = input("请输入查询的地区名字:")
    if chose in map:
        if len(map[chose])==0:
            print("没有内容了, 请返回上一层..")
        list.append(map)
        map = map[chose]
    if chose == 'b':
        if list == []:
            map = menu
        else:
            map = list[len(list)-1]
            list.pop()
    if chose == 'q':
        print("再见!")
        break
View Code
人若有恒 无所不成
原文地址:https://www.cnblogs.com/weihuchao/p/6647215.html