python之练习-三层菜单

今天练习编写显示3层城市名称并可以返回上一层以及退出程序。

Readme:

程序概述
1:程序名称为:menu_three.py
2:记录省,市,县的原始文件名为:areafile
3:areafile文件内容通过列表,字典形式保存各级地市信息
展示效果:
通过运行程序,可以查看每级菜单目录,并且可以通过<b>返回上一层或者通过<q>退出程序

流程图:

代码:

 1 menufile = 'areafile.TXT'
 2 
 3 with open(menufile) as f:
 4     file_content = f.read()
 5     dict_format = eval(file_content)
 6     Flag = True
 7     while Flag:
 8         pro_dict = {}
 9         for id,provice_name in enumerate(dict_format):
10             pro_dict[id+1] = provice_name
11             print (id+1,provice_name)
12         pro_id = input('请输入进入的省ID:<exit退出>')
13         if pro_id == 'exit':
14             Flag = False
15         while Flag:
16             city_dict = {}
17             for id,city_name in enumerate(dict_format[pro_dict[int(pro_id)]]):
18                 city_dict[id+1] = city_name
19                 print(id+1,city_name)
20             city_id = input('请输入进入的市ID:<输入b返回上一层,输入exit退出>')
21             if city_id == 'b':
22                 break
23             if city_id == 'exit':
24                 Flag = False
25             while Flag:
26                 for cunty in dict_format[pro_dict[int(pro_id)]][city_dict[int(city_id)]]:
27                     print (cunty)
28                 result = input('请输入b返回上一层或者输入exit退出')
29                 if result == 'b':
30                     break
31                 if result == 'exit':
32                     Flag = False
View Code
原文地址:https://www.cnblogs.com/zy6103/p/6750537.html