要求:

1.城市三级菜单,

2.输入正确的城市名进入下一级菜单,

3.过程中输入‘q’键退出,输入‘b’键返回上一级

data = {
'北京':{
'昌平':{
'沙河':['oldboy','test'],
'天通苑':['链家地产','我爱我家'],
},
'朝阳':{
'望京':['奔驰','陌陌'],
'国贸':['CICC','HP'],
'东直门':['Advern','飞信']
},
'海淀':{}

},
'山东':{
'德州':{},
'青岛':{},
'济南':{}
},
'广东':{
'东莞':{},
'常熟':{},
'佛山':{}
}
}
while True:
for i in data:
print(i)
choice = input('选择进入1,按q退出或者选择列表中的城市继续选择>>:')
if choice in data:
while True:
for i2 in data[choice]:
print(i2)
choice2 = input('选择进入2,按q退出,按b 返回上一层>>:')
if choice2 in data[choice]:
while True:
for i3 in data[choice][choice2]:
print(i3)
choice3 = input('选择进入3,按q退出,按b 返回上一层>>:')
if choice3 in data[choice][choice2]:
while True:
for i4 in data[choice][choice2][choice3]:
print(i4)
choice4 = input('已经是最后一层按q退出,按b 返回上一层>>:')
if choice4 == 'q':
exit()
elif choice4 == 'b':
break
elif choice3 == 'q':
exit()
elif choice3 == 'b':
break
elif choice2 == 'q':
exit()
elif choice2 == 'b':
break
elif choice == 'q':
exit()
elif choice2 == 'b':
break

#总结:这段代码的书写和思考顺序,
1.用for循环完成遍历,输出结果
2.用if,elif语句进行判断输入是否在其中,如果在继续遍历下一层,如果是q退出,如果是b返回上一层
3.while的使用:当我们完成本层的操作要返回上一层的时候,发现如果没有while,本层的结果会反馈到上层的
if 分支语句中,导致出错不是我们想要的结果

#关于while和 for 的辨析:
不知道循环次数,但有定循环条件时用while,
需要遍历结果的时候用for