Python实现简单的三级菜单

话不多说,直奔代码

# 要处理的字典
dic1 = {'北京': {'东城':{'沙河': ['沙河机场', '链家'],'天通苑': ['北方明珠', '天通尾货']},'朝阳':{'花家地': ['朝阳公园', '望京soho'],'北小河': ['北小河公园', '北京中学']}},'上海': {'虹桥':{'虹桥机场': ['超市', '特产店', '水吧'],'东方明珠': ['电影院', '游泳馆', '餐馆']},'浦东':{'景秀路': ['世纪公园', '立交桥'],'中环路': ['鲁迅公园', '同济大学']}},'河北': {'石家庄':{'行唐': ['东正', '阳关'],'赵县': ['赵州桥', '高村乡']},'唐山':{'滦南县': ['司各庄镇', '安各庄镇'],'玉田县': ['玉田镇', '亮甲店镇']}}
}

 

 

# 方法一:
tag=True
while tag:menu1=menufor key in menu1:print(key)choice1=input('The First Layer>>: ').strip()if choice1 == 'b':breakif choice1 == 'q':tag = Falsebreakif choice1 not in menu1:continuewhile tag:menu2=menu1[choice1] for key in menu2:print(key)choice2 = input('The Second Layer>>: ').strip()if choice2 == 'b':breakif choice2 == 'q':tag=Falsebreakif choice2 not in menu2:continuewhile tag:menu3 = menu2[choice2]             for key in menu3:print(key)choice3 = input('The Third Layer>>: ').strip() if choice3 == 'b':breakif choice3 == 'q':tag=Falsebreakif choice3 not in menu3: continuewhile tag:menu4 = menu3[choice3]                  for key in menu4:print(key)choice4 = input('The Fourth Layer>>: ').strip()if choice4 == 'b':breakif choice4 == 'q':tag=Falsebreakif choice4 not in menu4: continue

 

current_layer = dic1
parent_layer = []  # 用来放置父级的key组成的list
choose_end=['沙河机场', '链家','北方明珠', '天通尾货','朝阳公园', '望京soho','北小河公园', '北京中学','超市', '特产店', '水吧','电影院', '游泳馆', '餐馆','世纪公园', '立交桥','鲁迅公园', '同济大学','东正', '阳关','赵州桥', '高村乡','司各庄镇','安各庄镇','玉田镇', '亮甲店镇',]Tag = True
while Tag:print('\033[31m%s \033[0m' % '请输入序号'.ljust(20, '*'))print('\033[31m***输入back返回上一级菜单,或者quit退出***\033[0m')for i in current_layer:print(i)choose = input('>>>:').strip()if choose in current_layer:if choose in choose_end:print('\033[31m%s \033[0m' % '已经到最后一页'.center(40, '*'))continueelse:parent_layer.append(current_layer)current_layer = current_layer[choose]# else:#     print('\033[31m%s \033[0m' % '已经到最后一页'.center(40, '*'))elif choose == 'back':if len(parent_layer) != 0:current_layer = parent_layer.pop()else:print('\033[31m%s \033[0m' % '已经到达最上级菜单'.center(40, '*'))elif choose == 'quit':print('\033[31m Bye~Bye~^_^ \033[0m')breakelse:print('\033[31m%s \033[0m' % '输入有误,请重新输入'.center(40, '*'))

 

转载于:https://www.cnblogs.com/JetpropelledSnake/p/8648439.html


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部