三级菜单

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

三级菜单

闫灬夕   2019-11-17 我要评论

数据结构:

 1 menu = {
 2     '北京':{
 3         '海淀':{
 4             '五道口':{
 5                 'soho':{},
 6                 '网易':{},
 7                 'google':{}
 8             },
 9             '中关村':{
10                 '爱奇艺':{},
11                 '汽车之家':{},
12                 'youku':{},
13             },
14             '上地':{
15                 '百度':{},
16             },
17         },
18         '昌平':{
19             '沙河':{
20                 '老男孩':{},
21                 '北航':{},
22             },
23             '天通苑':{},
24             '回龙观':{},
25         },
26         '朝阳':{},
27         '东城':{},
28     },
29     '上海':{
30         '闵行':{
31             "人民广场":{
32                 '炸鸡店':{}
33             }
34         },
35         '闸北':{
36             '火车站':{
37                 '携程':{}
38             }
39         },
40         '浦东':{},
41     },
42     '山东':{},
43 }

 

需求:

  • 可依次选择进入各子菜单
  • 可从任意一层往回退到上一层
  • 可从任意一层退出程序
  • 所需知识点:列表、字典

 

源代码:

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

tag=True
while tag:
    for name1 in msg.keys():
        print(name1)
    sheng1 = input('1请输入省份>>:').strip()
    if sheng1 not in msg:
        print('暂无此省份,持续更新中。。。')
        continue
    while tag:
        for name2 in msg[sheng1]:
            print('%s' % (name2))
        sheng2 = input('2请输入省份>>:').strip()
        if sheng2 not in msg[sheng1]:
            print('暂无此省份,持续更新中。。。')
            user = input('退出菜单“Q”/返回上一层“E”\n继续则输入其他任意字符>>:').strip()
            if user.lower() == 'q':
                tag = False
                break
            elif user.lower() == 'e':
                break
            else:
                continue
        while tag:
            for name3 in msg[sheng1][sheng2]:
                print('%s' % (name3))
            sheng3 = input('3请输入省份>>:').strip()
            if sheng3 not in msg[sheng1][sheng2]:
                print('暂无此省份,持续更新中。。。')
                user = input('退出菜单“Q”/返回上一层“E”\n继续则输入其他任意字符>>:').strip()
                if user.lower() == 'q':
                    tag = False
                    break
                if user.lower() == 'e':
                    break
                else:
                    continue
            while tag:
                for name4 in msg[sheng1][sheng2][sheng3]:
                    print('%s' % (name4))
                sheng4 = input('4请输入省份>>:').strip()
                if sheng4 not in msg[sheng1][sheng2][sheng3]:
                    print('暂无此省份,持续更新中。。。')
                    user = input('退出菜单“Q”/返回上一层“E”\n继续则输入其他任意字符>>:').strip()
                    if user.lower() == 'q':
                        tag = False
                        break
                    if user.lower() == 'e':
                        break
                    else:
                        continue
                while tag:
                    for name5 in msg[sheng1][sheng2][sheng3][sheng4]:
                        print('%s' % (name5))
                    user = input('退出菜单“Q”/返回上一层“E”\n继续则输入其他任意字符>>:').strip()
                    if user.lower() == 'q':
                        tag = False
                        break
                    if user.lower() == 'e':
                        break
                    else:
                        continue

 

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们