基础篇
data = {
'浙江':{
'杭州':{
"西湖":['tea','silk'],
"上城区":['链家','我爱我家']
},
'宁波':{
"慈溪":['奔驰','默默'],
"宁海":['SCI','HP'],
"奉化":['Advent','Jewels']
}
},
'广东':{
"东莞":{},
"常熟":{},
"佛山":{}
},
}
Exit_flag = False
while not Exit_flag:
for Layer1 in data:
print(Layer1)
choice = input("Enter>>1:")
if choice in data:
while not Exit_flag:
for Layer2 in data[choice]:
print('\t',Layer2)
choice2 = input("Enter>>2:")
if choice2 in data[choice]:
while not Exit_flag:
for Layer3 in data[choice][choice2]:
print('\t\t', Layer3)
choice3 = input("Enter>>3:")
if choice3 in data[choice][choice2]:
for Layer4 in data[choice][choice2][choice3]:
print('\t\t\t',Layer4)
choice4 = input("The last Layer,press 'b' to back>>:" )
if choice4 == 'b':
pass
elif choice4 == 'q':
Exit_flag = True
if choice3 == 'b':
break
elif choice3 == 'q':
Exit_flag = True
if choice2 == 'b':
break
elif choice2 == 'q':
Exit_flag = True
elif choice == 'q':
Exit_flag = True
改进篇
'''
@author: XT
'''
data = {
'浙江':{
'杭州':{
"西湖":['tea','silk'],
"上城区":['链家','我爱我家']
},
'宁波':{
"慈溪":['奔驰','默默'],
"宁海":['SCI','HP'],
"奉化":['Advent','Jewels']
}
},
'广东':{
"东莞":{},
"常熟":{},
"佛山":{}
},
}
def init_first_layer():
for Layer in data:
print(Layer)
i = 1
choice = input("Enter>>{}:".format(i))
choice = choice.strip()
tabString_enter = '\t'
choice_enter(choice, data, i, tabString_enter, exit_flag=False)
def choice_enter(choice, c_data, i, tabString_enter, exit_flag):
while not exit_flag:
if choice in c_data:
for Layer in c_data[choice]:
print(tabString_enter, Layer)
while not exit_flag:
put_data = c_data[choice]
tabString_enter += '\t'
end = 4
i += 1
while i < end:
choice = input("Enter>>{}:".format(i))
choice = choice.strip()
if choice in put_data:
choice_enter(choice, put_data, i, tabString_enter, exit_flag=False)
elif choice == 'b':
init_first_layer()
elif choice == 'q':
exit_flag = True
exit()
else:
print('Please input again!')
choice = input("Enter again>>{}:".format(i))
choice = choice.strip()
else:
print('The last one! Back!')
choice = input("Enter again>>{}:".format(i))
choice = choice.strip()
if choice == 'q':
exit()
break
elif choice == 'b':
init_first_layer()
elif choice == 'q':
exit_flag = True
else:
print('Please input again!')
choice = input("Enter again>>{}:".format(i))
choice = choice.strip()
init_first_layer()