码迷,mamicode.com
首页 > 其他好文 > 详细

三级菜单作业,优化,保存在文件;

时间:2017-08-23 16:47:57      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:menu   choice   三级菜单   作业   logs   class   open   poe   des   

三级菜单作业:

1、菜单内容保存在文件 menu

  menu内容:

  {‘jiangsu‘: {‘wuxi‘: {‘宜兴‘:{‘官林‘:{}}, ‘jiangying‘:{}, ‘xishan‘:{}}, ‘nanjing‘: {‘qixia‘, ‘gulou‘, ‘changning‘, ‘zhongshan‘}},
‘zhejiang‘: {‘嘉兴‘: {‘a2‘, ‘a3‘, ‘a4‘},‘宁波‘: {‘a1‘, ‘a2‘, ‘a3‘}},
‘guangzhou‘: {1: {‘a‘}, 2: {‘b‘}, 3:{‘c‘}}
}  

2、用函数取menu内容,转化内容为字典格式;   函数: eval()

3、菜单优化方式做逻辑结构;

 

with open(menu) as poem:
    f=poem.read()
dic_city=eval(f)
# print(type(dic_city))
last_layer=[]
current_layer=dic_city
while True:
    for i in current_layer:
        print(i)
    choice = input(pls input your next_destion:).strip()
    if len(choice)==0:continue
    if choice in current_layer:
        # print(choice)
        last_layer.append(current_layer)
        current_layer = current_layer[choice]
        # print(last_layer)
        # print(current_layer)
    if choice==b and last_layer:
        # if last_layer:
        current_layer = last_layer[-1]
        last_layer.pop()

 

三级菜单作业,优化,保存在文件;

标签:menu   choice   三级菜单   作业   logs   class   open   poe   des   

原文地址:http://www.cnblogs.com/santizhou/p/7418871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!