优化了上一个三级菜单选择城:http://www.cnblogs.com/klvchen/p/8646466.html
info = {
'GuangDong':{
'GuangZhou': ['TianHe', 'HaiZhu'],
'MaoMing': ['MaoNan', 'DianBai']},
'ShanDong': {
'JiNan': ['ShiZhong', 'LiXia'],
'QingDao': ['ShiNan', 'ShiBei']
},
'GuangXi': {
'NanNin': ['WuNing', 'LongAn'],
'GuiLing': ['YangSuo', 'QuanZhou']
}
}
parent_list = []
current_layer = info
while True:
for key in current_layer:
print(key)
choiced = input("Please select: [ b: back | q: exit]")
if choiced in current_layer:
parent_list.append(current_layer)
try:
current_layer = current_layer[choiced]
except:
print("This is last level")
current_layer = parent_list.pop()
continue
elif choiced == "b":
if parent_list:
current_layer = parent_list.pop()
elif choiced == "q":
break
else:
print("Please input right optional.")
continue
运行结果: