本文共 1129 字,大约阅读时间需要 3 分钟。
优化low的三级菜单https://blog.51cto.com/000011211684/1980529,
以下内容是参考python学习视频整理。#!/usr/bin/env python#Author:Li QWregion_data={ 'china':{ 'beijing':{ '昭阳区':['中手游','万家娱乐'], '五环内':['长城','故宫'] }, 'nanjing':{ '建邺区':['奥体中心','南京眼'], '鼓楼区':['南京师范','南艺'] }, 'shenzhen':{ '龙华新区':['百货','肠粉'], '福田区':['中康路','南京路'] }, }, 'USA':{ '旧金山':{ '1':['11','111'], '2':['22','222'] }, '硅谷':{ }, '纽约':{ }, }}level=[]while True: for info in region_data: print(info) choice=input("Input choice >>> ") if choice == "b": if len(level)==0:break #当列表为空就直接退出循环 region_data=level[-1] #将父值,赋值给当前列表 level.pop() ##记得删除(pop默认移除最后一个元素,并返回) if len(choice) ==0 or choice not in region_data :continue #当输入值为空或者不存在,重新循环 level.append(region_data) #精华部分1:让记得老子是谁,方便退出。 region_data=region_data[choice] #精华部分2:把子菜单赋值给列表,方便进入下一层(记录当前位置,重新赋值给info,达到打印下一层目的)
转载于:https://blog.51cto.com/000011211684/2045855