学习Python的第二十天三级菜单高级版
Posted 憨憨科技
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习Python的第二十天三级菜单高级版相关的知识,希望对你有一定的参考价值。
------------恢复内容开始------------
三级菜单要求:
1.可以一层一层的进入到所有层;
2.可以在每一层返回上一层;输入b
3.可以在任意层退出 主菜单;输入q
#初级:(原始代码)
#__author:"hanhankeji" #date: 2019/12/11 menu = { "北京":{ "朝阳":{ "国贸":{ "CICC":{}, "hp":{}, "CCTV":{}, }, }, "望京":{ "momo":{}, "奔驰":{}, "360":{}, "联邦":{}, }, "三里屯":{ "优衣库":{}, "大使馆":{}, "酒吧":{}, "苹果":{}, }, "昌平":{ "沙河":{ "老男孩":{}, "包子铺":{}, }, "天通苑":{ "链家":{}, "我爱我家":{}, "汽修厂":{}, }, "回龙观":{}, "海定":{ "五道口":{ "谷歌":{}, "搜狐":{}, "网易":{}, "搜狗":{}, "快手":{}, "清华":{}, "北大":{}, }, "中关村":{ "优酷":{}, "爱奇艺":{}, "腾讯":{}, "汽车之家":{}, "新东方":{}, "炸鸡店":{}, "南翔":{}, }, }, }, }, "上海":{ "浦东区":{ "陆家嘴":{ "CICC":{}, "高盛":{}, "摩根":{}, }, "外滩":{}, "东方明珠":{}, }, "闸北区":{}, "黄浦区":{}, "徐汇区":{}, "嘉定区":{}, }, "山东":{ "济南":{ "市中区":{}, "历下区":{}, "天桥区":{}, "桂英渠":{}, "长清区":{}, }, "德州":{}, "青岛":{}, "泰安":{}, "枣庄":{}, }, "江苏":{ "南京市":{}, "苏州市":{}, "常州":{}, "张家港市":{}, "大丰市":{}, }, "重庆":{}, "浙江":{}, } back_flag = False exit_falg = False while not back_flag and not exit_falg : for i in (menu): print(i) choice = input("输入你的省>>:").strip() if choice == "q": exit_falg = True if choice in menu : while not back_flag and not exit_falg: for j in menu[choice]: print(j) choice2 = input("输入你的县级>>:").strip() if choice2 == "q": exit_falg = True if choice2 == "b": back_flag = True if choice2 in menu[choice]: while not back_flag and not exit_falg: for k in menu[choice][choice2]: print(k) choice3 = input("输入你的市级>>:") if choice3 == "q": exit_falg = True if choice3 == "b": back_flag = True if choice3 in menu[choice][choice2]: while not back_flag and not exit_falg: for l in menu[choice][choice2][choice3]: print(l) choice4 = input("输入区级>>:").strip() print(" last leavel!") if choice4 == "b" : back_flag = True if choice4 == "q" : exit_falg = True else: back_flag =False else: back_flag = False else: back_flag = False
高级版本:
#__author:"hanhankeji" #date: 2019/12/17 menu = { "北京":{ "朝阳":{ "国贸":{ "CICC":{}, "hp":{}, "CCTV":{}, }, }, "望京":{ "momo":{}, "奔驰":{}, "360":{}, "联邦":{}, }, "三里屯":{ "优衣库":{}, "大使馆":{}, "酒吧":{}, "苹果":{}, }, "昌平":{ "沙河":{ "老男孩":{}, "包子铺":{}, }, "天通苑":{ "链家":{}, "我爱我家":{}, "汽修厂":{}, }, "回龙观":{}, "海定":{ "五道口":{ "谷歌":{}, "搜狐":{}, "网易":{}, "搜狗":{}, "快手":{}, "清华":{}, "北大":{}, }, "中关村":{ "优酷":{}, "爱奇艺":{}, "腾讯":{}, "汽车之家":{}, "新东方":{}, "炸鸡店":{}, "南翔":{}, }, }, }, }, "上海":{ "浦东区":{ "陆家嘴":{ "CICC":{}, "高盛":{}, "摩根":{}, }, "外滩":{}, "东方明珠":{}, }, "闸北区":{}, "黄浦区":{}, "徐汇区":{}, "嘉定区":{}, }, "山东":{ "济南":{ "市中区":{}, "历下区":{}, "天桥区":{}, "桂英渠":{}, "长清区":{}, }, "德州":{}, "青岛":{}, "泰安":{}, "枣庄":{}, }, "江苏":{ "南京市":{}, "苏州市":{}, "常州":{}, "张家港市":{}, "大丰市":{}, }, "重庆":{}, "浙江":{}, } current_layer = menu #实现动态循环 parent_layers = [] #保存所有父级, 最后一个元素永远都是父级 # parent_list = [] while True: for i in current_layer: print(i) choice = input("请输入选项>>:").strip() if len(choice) == 0 : continue if choice in current_layer : # parent_layers = current_layer[choice] #改之前相当于父亲 parent_layers.append(current_layer) #在进入下一层之前,把当前层(也就是下一层的父级)追加到列表中 #下一次loop,当用户选择b的时候后,就可以直接取列表的最后一个值出来就OK了· current_layer = current_layer[choice] #改成了子层 elif choice == "b": #current_layer = parent_layers.pop() if parent_layers : #[] current_layer = parent_layers.pop()#取出列表的最后一个值 # current_layer = parent_layers #把子层改成父亲 elif choice == "q" : break else: print("没有这个选项,请确认!")
------------恢复内容结束------------
以上是关于学习Python的第二十天三级菜单高级版的主要内容,如果未能解决你的问题,请参考以下文章