Python实践之路2——三级菜单

一、代码需求:

编写一个三级菜单:

1、可依次进入各菜单;

2、输入 “back” 可返回上一级菜单;

3、每一级菜单输入 “quit” 可退出程序。

二、程序代码:

第一版代码:

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 province = ['河南', '陕西']
 6 city = ['郑州', '洛阳', '三门峡', '西安', '渭南', '宝鸡']
 7 city_henan = ['郑州', '洛阳', '三门峡']
 8 city_shanxi = ['西安', '渭南', '宝鸡']
 9 country_zhengzhou = ['金水区', '二七区', '惠济区', '中原区', '上街区']
10 country_luoyang = ['涧西区', '西工区', '老城区', '洛龙区', '吉利区']
11 country_sanmenxia = ['灵宝市', '卢氏', '义马市', '陕县']
12 country_xian = ['碑林区', '莲湖区', '雁塔区', '未央区', '灞桥区']
13 country_weinan = ['临渭区', '华阴市', '韩城', '华县', '潼关']
14 country_baoji = ['金台区', '渭滨区', '岐山县']
15 
16 province_city = {
17     '河南' : city_henan,
18     '陕西' : city_shanxi
19 }
20 city_country = {
21     '郑州' : country_zhengzhou,
22     '洛阳' : country_luoyang,
23     '三门峡': country_sanmenxia,
24     '西安' : country_xian,
25     '渭南' : country_weinan,
26     '宝鸡' : country_baoji
27 }
28 T = True
29 T1 = True
30 while T:
31     print(province)
32     flag = True
33     province_input =  input('Please choose one province to find out or input exit to quit!')
34     if province_input == 'exit':
35         break
36     else:
37         for i  in province:
38             if province_input == i:
39                 print(province_city[i])
40                 i1=i
41                 flag = False
42         if flag:
43             print('Cheak your input')
44             continue
45         while T1:
46             city_input = input('Please choose one city to find out '
47                                'or input back to last menu or input exit to quit!')
48             if city_input == 'exit':
49                 T = False
50                 T1 = False
51             elif city_input == 'back':
52                 break
53             else:
54                 for j in city:
55                     if city_input == j:
56                         print(city_country[j])
57                         country_input = input('Please input back to last menu or input exit to quit!')
58                         if country_input == 'exit':
59                             T = False
60                             T1 = False
61                         elif country_input == 'back':
62                             print(province_city[i1])
63                             break
64 print ('Have Fun !')

 第二版代码:

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 #定义三级菜单
 6 menu = {
 7     '河南': {
 8         '郑州': ['金水区', '二七区', '惠济区', '中原区', '上街区'],
 9         '洛阳': ['涧西区', '西工区', '老城区', '洛龙区', '吉利区'],
10         '三门峡' : ['灵宝市', '卢氏', '义马市', '陕县']
11     },
12     '陕西': {
13         '西安': ['碑林区', '莲湖区', '雁塔区', '未央区', '灞桥区'],
14         '渭南': ['临渭区', '华阴市', '韩城', '华县', '潼关'],
15         '宝鸡': ['涧西区', '西工区', '老城区', '洛龙区', '吉利区']
16     }
17 }
18 
19 continue_flag = True
20 
21 while continue_flag:
22     for i in menu:
23         print(i)
24     province_input = input('Please choose one province to find out or input exit to quit!')
25     if province_input in menu:
26         #因为每级菜单都需要反复进入,故引入循环嵌套
27         while continue_flag:
28             for i1 in menu[province_input]:
29                 print(i1)
30             city_input = input('Please choose one city to find out \
31 or input back to last menu or input exit to quit!')
32             if city_input in menu[province_input]:
33 
34                 while continue_flag:
35                     for i2 in menu[province_input][city_input]:
36                         print(i2)
37                     country_input = input('Please input back to last menu or input exit to quit!')
38                     if country_input == 'back':
39                         break
40                     elif country_input == 'quit':
41                         continue_flag = False
42                     else:
43                         print("Please check your input!")
44 
45             elif city_input == 'back':
46                 break
47             elif city_input == 'quit':
48                 continue_flag = False
49             else:
50                 print("Please check your input!")
51 
52     elif province_input == 'quit':
53         continue_flag = False
54     else:
55         print("Please check your input!")

输出结果:

 1 E:\Python\PythonExercising\Menu\venv\Scripts\python.exe E:/Python/PythonExercising/Menu/3level_menu.py
 2 河南
 3 陕西
 4 Please choose one province to find out or input exit to quit!上海
 5 Please check your input!
 6 河南
 7 陕西
 8 Please choose one province to find out or input exit to quit!河南
 9 郑州
10 洛阳
11 三门峡
12 Please choose one city to find out or input back to last menu or input exit to quit!三门峡
13 灵宝市
14 卢氏
15 义马市
16 陕县
17 Please input back to last menu or input exit to quit!back
18 郑州
19 洛阳
20 三门峡
21 Please choose one city to find out or input back to last menu or input exit to quit!洛阳
22 涧西区
23 西工区
24 老城区
25 洛龙区
26 吉利区
27 Please input back to last menu or input exit to quit!quit
28 
29 Process finished with exit code 0

总结提高: 

第二版代码相比第一版代码:

1、熟悉多级字典的写法;

2、加入 in 用法判断列表或字典的元素是否在其中,优化判断逻辑;

3、因为每级菜单都需要反复进入,引入循环嵌套;

4、可以通过引入循环标志位来同时跳出多级循环。

 

posted @ 2018-03-22 02:00  VisonWong  阅读(267)  评论(0编辑  收藏  举报