三級菜單作業,優化,保存在文件;
阿新 • • 發佈:2017-08-23
pen 三級菜單 current hang 轉化 put ext spa cit
三級菜單作業:
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 incurrent_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()
三級菜單作業,優化,保存在文件;