#三级菜单实例
data = {
'beijing':{
"daoxianghu":{
"renhang":["mon","ey"],
"nonghang":["fnag","ban"]
},
"xierqi":{
"bai":["ditu","tieba"],
"lian":{"lenovo","thinkpad"},
"di":{"didi","didu"},
},
"daxing":{},
},
'xianggang':{
"jiulong":{},
"wangjiao":{},
"xinjie":{}
},
'guangdong':{
"dongg":{},
"zhuhai":{},
"foshan":{},
},
}
#定义一个exit_flag标志位
exit_flag = False
#当条件为真时,打印第一层菜单
while not exit_flag:
for i in data:
print(i)
choice = input("选择进入1>>:")
#判断在不在,如果在就打印第二层菜单
if choice in data:
while not exit_flag:
for i2 in data[choice]:
print("",i2)
choice2 = input("选择进入2>>:")
#判断在不在,如果在就打印第三层菜单
if choice2 in data[choice]:
while not exit_flag:
for i3 in data[choice][choice2]:
print("", i3)
choice3 = input("选择进入3>>:")
#进入第四层菜单,如果不存在就按b返回,按q退出
if choice3 in data[choice][choice2]:
for i4 in data[choice][choice2][choice3]:
print("",i4)
choice4 = input("最后一层,按b返回>>:")
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
图片
感谢阅读,欢迎在评论区中发表自己不同的观点,若有其他问题请在评论区留言,喜欢的朋友请多多关注转发支持一下。
领取专属 10元无门槛券
私享最新 技术干货