三级菜单作业:
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()
时间: 2024-11-04 10:04:39