码迷,mamicode.com
首页 > 其他好文 > 详细

进入三级目录习题讲解

时间:2017-03-30 19:40:15      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:alt   输入   目录   hose   play   bre   山东   老男孩   lis   

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

进入三级目录习题讲解

标签:alt   输入   目录   hose   play   bre   山东   老男孩   lis   

原文地址:http://www.cnblogs.com/weihuchao/p/6647215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!