省市区县,实现逐层访问,并逐层返回!

data = {
'北京': {
'朝阳': {},
'海淀': {},
'昌平': {},
},
'上海': {},
'四川': {
'广元':{
'利州区':{},
'昭化':{},
'朝天三区':{},
},
'成都':{
'金牛区': {},
'成华区': {},
'高新区': {},
},
'绵阳':{
'涪城区': {},
'游仙区': {},
'农科区': {},
},
},
}

current_layer = data
parent_layers = []
while True:
for i in current_layer: #循环字典中的key
print(i)
choice = input('请输入:')
if choice in current_layer:
parent_layers.append(current_layer)
current_layer = current_layer[choice]
elif choice == 'b':
if parent_layers:
current_layer = parent_layers.pop()
elif choice == 'q':
current_layer = data
elif choice == 'e':
exit()
else:
print('您的输入有误,请重新输入:')

posted on 2017-10-20 18:00  舞曲  阅读(167)  评论(0编辑  收藏  举报

导航