🐍Python三级菜单作业实现

数据结构:

 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 }

 

作业需求:

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

 

源代码:

  1 msg =  {
  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 }
 44 
 45 tag=True
 46 while tag:
 47     for name1 in msg.keys():
 48         print(name1)
 49     sheng1 = input('1请输入省份>>:').strip()
 50     if sheng1 not in msg:
 51         print('暂无此省份,持续更新中。。。')
 52         continue
 53     while tag:
 54         for name2 in msg[sheng1]:
 55             print('%s' % (name2))
 56         sheng2 = input('2请输入省份>>:').strip()
 57         if sheng2 not in msg[sheng1]:
 58             print('暂无此省份,持续更新中。。。')
 59             user = input('退出菜单“Q”/返回上一层“E”\n继续则输入其他任意字符>>:').strip()
 60             if user.lower() == 'q':
 61                 tag = False
 62                 break
 63             elif user.lower() == 'e':
 64                 break
 65             else:
 66                 continue
 67         while tag:
 68             for name3 in msg[sheng1][sheng2]:
 69                 print('%s' % (name3))
 70             sheng3 = input('3请输入省份>>:').strip()
 71             if sheng3 not in msg[sheng1][sheng2]:
 72                 print('暂无此省份,持续更新中。。。')
 73                 user = input('退出菜单“Q”/返回上一层“E”\n继续则输入其他任意字符>>:').strip()
 74                 if user.lower() == 'q':
 75                     tag = False
 76                     break
 77                 if user.lower() == 'e':
 78                     break
 79                 else:
 80                     continue
 81             while tag:
 82                 for name4 in msg[sheng1][sheng2][sheng3]:
 83                     print('%s' % (name4))
 84                 sheng4 = input('4请输入省份>>:').strip()
 85                 if sheng4 not in msg[sheng1][sheng2][sheng3]:
 86                     print('暂无此省份,持续更新中。。。')
 87                     user = input('退出菜单“Q”/返回上一层“E”\n继续则输入其他任意字符>>:').strip()
 88                     if user.lower() == 'q':
 89                         tag = False
 90                         break
 91                     if user.lower() == 'e':
 92                         break
 93                     else:
 94                         continue
 95                 while tag:
 96                     for name5 in msg[sheng1][sheng2][sheng3][sheng4]:
 97                         print('%s' % (name5))
 98                     user = input('退出菜单“Q”/返回上一层“E”\n继续则输入其他任意字符>>:').strip()
 99                     if user.lower() == 'q':
100                         tag = False
101                         break
102                     if user.lower() == 'e':
103                         break
104                     else:
105                         continue

 

 

 测试:

posted @ 2019-11-17 13:45  给你骨质唱疏松  阅读(315)  评论(0编辑  收藏  举报