三级菜单

 #三级菜单要求:

'''要求:
1、打印省、市、县三级菜单
2、可返回上一级
3、可随时退回程序'''
基本版:
 1 #_author_:'alex zhou'
 2 # Time: 2018/8/31 0031
 3 menu={
 4    3、可随时退回程序'''
 5 
 6        '江苏':{
 7                '徐州':{
 8                         '睢宁':{},
 9                         '云龙':{},
10                         '泉山':{},
11                         '铜山':{}
12                      },
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 
46        '浙江':{
47 
48                 '杭州':{
49                          '西湖':{},
50 
51                           '上城':{}
52 
53 
54                       },
55 
56                '温州':{
57                        '永康':{},
58 
59                        '瓯海':{}
60 
61                      }
62 
63 
64               },
65      }
66 exit_flag=False
67 while  not exit_flag:
68     for key in menu:
69         print(key)
70     choice1=input('>>>请选择您的省份或者退出[q]:')
71     if choice1.strip()=='q':
72         exit_flag=True
73     elif choice1.strip() in menu:
74         while not exit_flag:
75             for key2 in menu[choice1]:
76                 print(key2)
77             choice2=input('>>>请选择您所在的市或者返回上一层[b]或者退出[q]:')
78             if choice2.strip() in menu[choice1]:
79                 while not  exit_flag:
80                     for key3 in menu[choice1][choice2]:
81                         print(key3)
82                     choice3=input('请选择您所在的县或者返回上一层[b]或者退出[q]:')
83                     if choice3.strip() in menu[choice1][choice2]:
84                             print(choice3)
85                     elif choice3.strip()=='q':
86                         exit_flag=True
87                     elif choice3.strip()=='b':
88                         break
89                     else:
90                         print('没有该县,重新选择:')
91             elif choice2.strip()=='q':
92                 exit_flag=True
93             elif choice2.strip()=='b':
94                 break
95             else:
96                 print('没有该市,重新选择')
97     else:
98         print('没有该省,重新选择')
View Code


升级版(循环嵌套):
 1 #_author_:'alex zhou'
 2 # Time: 2018/8/31 0031
 3 menu={
 4 
 5        '江苏':{
 6                '徐州':{
 7                         '睢宁':{},
 8                         '云龙':{},
 9                         '泉山':{},
10                         '铜山':{}
11                      },
12 
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        '浙江':{
46 
47                 '杭州':{
48                          '西湖':{},
49 
50                           '上城':{}
51 
52 
53                       },
54 
55                '温州':{
56                        '永康':{},
57 
58                        '瓯海':{}
59 
60                      }
61 
62 
63               },
64      }
65 current_layer=menu  #当前菜单
66 parent_list=[]  #存放父亲,记录上一层,记得及时删除
67 while True:
68     for key in current_layer:
69         print(key)
70     choice=input('请输入您的选择:')
71     if choice.strip() in current_layer:
72         parent_list.append(current_layer)
73         #在进入下一层之前,把当前层(也就是下一层父级)存储,
74         #下一次loop的时候,当用户选择b的时候,就可以直接取列表的最后一个值
75         current_layer=current_layer[choice]
76     elif choice=='q':
77         break
78     elif choice=='b':
79         #current_layer=parent_list[len(parent_list)-1] #取父亲列表赋值
80         #parent_list.pop()                 #删除父亲列表最后一个元素
81         current_layer = parent_list.pop()  #取出当前层的父层实现后退
82     else:
83         print('您输入有错误')
View Code

 



 

 
posted @ 2018-08-31 18:59  aled1  阅读(143)  评论(0)    收藏  举报