python实现三级菜单联动
程序需求
1. 能够显示每级菜单
2. 每级菜单中能够返回到上级
3. 每级菜单中能够退出
代码实现
1 menu = { 2 "湖北省":{ 3 "武汉市":["江岸区", "江汉区", "汉阳区"], 4 "荆门市":["东宝区", "京山县" ], 5 "荆州市":["沙市区", "荆州区","江陵县"] 6 }, 7 "河北省":{ 8 "保定市":["定州市", "唐县", "徐水区", "清苑区"], 9 "唐山市":["路南区", "路北区", "遵化市"] 10 }, 11 "山东省":{ 12 "淄博市":["张店区", "淄川区"], 13 "青岛市":["市南区", "市北区", "黄岛区"], 14 "济南市":["历下区", "市中区", "天桥区", "章丘区"], 15 "烟台市":["莱州市", "蓬莱市", "海阳市"] 16 } 17 } 18 19 while True: 20 for key in current_layer: 21 print(key) 22 choice = input(">>>:").strip() 23 if len(choice) == 0: 24 continue 25 if choice in current_layer: 26 parent_list.append(current_layer) # 修改之前的层数 27 print(parent_list) 28 current_layer = current_layer[choice] # 修改之后的子层 29 elif choice == "b": 30 if parent_list: 31 current_layer = parent_list.pop() 32 else: 33 print("无此项!")
思路来源
来源网络自学
浙公网安备 33010602011771号