三级目录
思路:
1.变量存放:由于本次数据有键值对的相关性,因此选用字典来存储变量
2.采用函数方式,首先建立输出一级菜单,二级菜单,三级菜单函数
3.显示逻辑:分不同的情况显示不同级菜单
# encoding:utf-8 dict = { '北京': { '朝阳': ['水立方', '巢'], '海淀': ['清华', '北大'], '昌平': ['天安门', '公主坟'] }, '上海': { '沙河': ['同济大学'], '天通苑': ['aa'], '回龙观': ['bb'] }, '西安': { '高新': ['锦业一路'], '雁塔': ['边家村'], '新城': ['革命公园'] } } firstmenu=[] secondmenu=[] def menu2(): print(dict.keys()) print(list(dict.keys())) print(dict['北京'].keys()) print(dict['上海'].keys()) print(dict['北京']['海淀']) print(dict['北京']['海淀'][0]) def show_firstmenu(): global firstmenu firstmenu = list(dict.keys()) print('--------firstmenu--------') for item in firstmenu: print(firstmenu.index(item)+1,item) print('请选择进入下一级目录,或输入q(quit)退出') # print(firstmenu) return firstmenu def show_secondmenu(args): global secondmenu arg1=firstmenu[args-1] secondmenu=list(dict[arg1].keys()) print('---------secondmenu-------') for item in secondmenu: print(secondmenu.index(item)+1,item) print('请选择,或输入b(back)返回上级菜单,或输入q(quit)退出') return secondmenu def show_thirdmenu(args,args1): S1=firstmenu[args-1] S2=secondmenu[args1-1] thirdmenu=dict[S1][S2] print('---------thirdmenu---------') for item in thirdmenu: print(thirdmenu.index(item)+1,item) print('最后一级目录,请输入b(back)返回上级菜单,或输入q(quit)退出') def operate_menu(): while True: show_firstmenu() firstInput = input('>>:') if firstInput == 'q': break elif firstInput.isdigit(): firstInput=int(firstInput) if firstInput>0 and firstInput<=3: while True: show_secondmenu(firstInput) secondInput=input('>>:') if secondInput=='q': exit() elif secondInput=='b': show_firstmenu() break elif secondInput.isdigit(): secondInput=int(secondInput) if int(secondInput)>0 and int(secondInput)<=3: while True: show_thirdmenu(firstInput,int(secondInput)) third_Input=input(">>:") if third_Input=='q': exit() elif third_Input=='b': show_secondmenu(firstInput) break else: print('非法输入') else: print('没有该选项,请重新输入') else: print('非法输入') else: print('没有该选项,请重新输入') else: print('非法输入') operate_menu()