三级菜单

用递归实现:

menu = { '北京': { '海淀': { '五道口': { 'soho': {}, '网易': {}, 'google': {} }, '中关村': { '爱奇艺': {}, '汽车之家': {}, 'youku': {}, }, '上地': { '百度': {}, }, }, '昌平': { '沙河': { '老男孩': {}, '北航': {}, }, '天通苑': {}, '回龙观': {}, }, '朝阳': {}, '东城': {}, }, '上海': { '闵行': { "人民广场": { '炸鸡店': {} } }, '闸北': { '火车战': { '携程': {} } }, '浦东': {}, }, '山东': {}, } def threeMenu(dic): while 1: for k in dic: print(k) key = input('输入城市(按b返回上一层,按q退出):') if key == 'b' or key == 'q': return key elif key in dic.keys() and dic[key]: ret = threeMenu(dic[key]) if ret == 'q': return ret elif (not dic.get(key)) or (not dic[key]): continue
menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车战': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}
def threeMenu(dic):
    while 1:
        for k in dic:
            print(k)            # 北京 上海 山东
        key = input('输入城市(按b返回上一层,按q退出):')          # 假设输入 北京
        if key == 'b' or key == 'q': return key
        elif key in dic.keys() and dic[key]:
            ret = threeMenu(dic[key])       # ---------------------------------- 调用
            if ret == 'q': return ret
        elif (not dic.get(key)) or (not dic[key]):
            continue

--------------------------------------------------------------------------------------------------------------------------------------------------

dic = {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
def threeMenu(dic):
    while 1:
        for k in dic:
            print(k)               # 海淀 昌平 朝阳 东城
        key = input('输入城市(按b返回上一层,按q退出):')      # 假设输入 昌平
        if key == 'b' or key == 'q': return key
        elif key in dic.keys() and dic[key]:
            ret = threeMenu(dic[key])       # ---------------------------------- 调用           回来的时候 ret = b
            if ret == 'q': return ret
        elif (not dic.get(key)) or (not dic[key]):
            continue

--------------------------------------------------------------------------------------------------------------------------------------------------

dic =  {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
def threeMenu(dic):
    while 1:
        for k in dic:
            print(k)                # 沙河 天通苑 回龙观
        key = input('输入城市(按b返回上一层,按q退出):')      # 假设输入 回龙观(一直继续)  输入‘b’
        if key == 'b' or key == 'q': return key          # return 'b'
        elif key in dic.keys() and dic[key]:
            ret = threeMenu(dic[key])
            if ret == 'q': return ret
        elif (not dic.get(key)) or (not dic[key]):
            continue
分步解析

 用堆栈实现:

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

l = [menu]
while l:
    for k in l[-1]:
        print(k)
    key = input('输入城市(按b返回上一层,按q退出):')
    if key in l[-1].keys() and l[-1][key]:
        l.append(l[-1][key])
    elif key == 'b':
        l.pop()
    elif key == 'q':
        break
    else:
        continue

 

 

posted @ 2018-07-28 11:14  李培冠  阅读(345)  评论(0编辑  收藏  举报