1 #三级菜单字典 2 district = { 3 "shanxi":{ 4 "xian":{"huxian":1,"baqiao":2}, 5 "hanzhong":{"yangxian":3,"chengu":4,"xixiang":5}, 6 "baoji":{"qishan":6,"fufeng":7,"meixian":8}, 7 }, 8 "guangdong":{ 9 "shenzhen":{"A":9,"B":10,"C":11}, 10 "guangzhou":{"E":12,"F":13,"G":14}, 11 "huizhou":{"X":15,"Y":16,"Z":17} 12 }
1 #递归实现三级菜单 2 def menu(all_menu): 3 while True: 4 for sub_menu in all_menu: 5 print sub_menu,":" 6 choose = raw_input("please choose menu:") 7 if choose in all_menu and isinstance(all_menu[choose],Iterable): 8 menu(all_menu[choose]) 9 elif choose == "b": 10 break 11 elif choose == "q": 12 exit()
1 #使用嵌套循环实现三级菜单 2 while True: 3 for first in district: 4 print first,":" 5 choose1 = raw_input("please choose menu1:") 6 if choose1 in district: 7 while True: 8 for second in district.get(choose1): 9 print second,":" 10 choose2 = raw_input("please choose menu2:") 11 if choose2 in district.get(choose1): 12 while True: 13 for third in district.get(choose1).get(choose2): 14 print third 15 choose3 = raw_input("please choose menu3:") 16 if choose3 in district.get(choose1).get(choose2): 17 pass 18 elif choose3 == "b": 19 break 20 elif choose3 == "q": 21 exit() 22 elif choose2 == "b": 23 break 24 25 elif choose2 == "q": 26 exit() 27 elif choose1 == "q": 28 exit()