python作业三之三级菜单

作业题目: 三级菜单

  • 作业需求:

    数据结构:
    menu = {
        '北京':{
            '海淀':{
                '五道口':{
                    'soho':{},
                    '网易':{},
                    'google':{}
                },
                '中关村':{
                    '爱奇艺':{},
                    '汽车之家':{},
                    'youku':{},
                },
                '上地':{
                    '百度':{},
                },
            },
            '昌平':{
                '沙河':{
                    '老男孩':{},
                    '北航':{},
                },
                '天通苑':{},
                '回龙观':{},
            },
            '朝阳':{},
            '东城':{},
        },
        '上海':{
            '闵行':{
                "人民广场":{
                    '炸鸡店':{}
                }
            },
            '闸北':{
                '火车战':{
                    '携程':{}
                }
            },
            '浦东':{},
        },
        '山东':{},
    }
    需求: 可依次选择进入各子菜单 可从任意一层往回退到上一层 可从任意一层退出程序 所需新知识点:列表、字典
  • 踩分点:

    1.完成需求/条---25分
    2.只用一个while循环,且整体代码量少于15行.
  • menu = {
        '北京':{
            '海淀':{
                '五道口':{
                    'soho':{},
                    '网易':{},
                    'google':{}
                },
                '中关村':{
                    '爱奇艺':{},
                    '汽车之家':{},
                    'youku':{},
                },
                '上地':{
                    '百度':{},
                },
            },
            '昌平':{
                '沙河':{
                    '老男孩':{},
                    '北航':{},
                },
                '天通苑':{},
                '回龙观':{},
            },
            '朝阳':{},
            '东城':{},
        },
        '上海':{
            '闵行':{
                "人民广场":{
                    '炸鸡店':{}
                }
            },
            '闸北':{
                '火车战':{
                    '携程':{}
                }
            },
            '浦东':{},
        },
        '山东':{},
    }
    
    new_file = menu
    file=[]
    while True:
        for i in new_file:
            print(i)
        choice = input(">>:").strip()
        if not choice: continue
        if choice in new_file:
            file.append(new_file)
            new_file = new_file[choice]
        elif choice=='b':
            if len(file)!=0:
                new_file = file.pop()
            else:
                print("顶层")
        elif choice=='q':
            print("退出")
            break

      方法二

  • l = [menu]
    while l:
        for key in l[-1]:print(key)
        k = input("input>>:").strip()
        if k in l[-1].keys() and l[-1][k]:l.append(l[-1][k])
        elif k == 'b':l.pop()
        elif k == 'q':break
        else:continue
    

      方法三

  •  

    def threeLM(dic):
            while True:
                for k in dic:print(k)
                key = input("input>>:").strip()
                if key == 'b' or key == 'q':return key
                elif key in dic.keys() and dic[key]:
                    ret = threeLM(dic[key])
                    if ret == 'q':return 'q'
    threeLM(menu)
    

      

      

  • 路飞学城代码

  • import sys
    menu = {'北京': {'海淀': {'五道口': {'soho': {}, '网易': {}, 'google': {}}, '中关村': {'爱奇艺': {}, '汽车之家': {}, 'youku': {}, }, '上地': {'百度': {}, }, }, '昌平': {'沙河': {'老男孩': {}, '北航': {}, }, '天通苑': {}, '回龙观': {}, }, '朝阳': {}, '东城': {}, }, '上海': {'闵行': {"人民广场": {'炸鸡店': {}}}, '闸北': {'火车战': {'携程': {}}}, '浦东': {}, }, '山东': {},}
    this_layer = menu
    while True:
        choice = input("1.若列表非空:你可选择的有%s或返回上层[back]或退出[quit];\n2.若列表为空:你可以返回上层[back]或退出[quit]否则自动退出\n3.选择错误程序会自动退出\n--->你的选择为:" % [x for x in this_layer.keys()])
        if (not this_layer and choice != "back" and choice != "quit") or (choice not in this_layer.keys() and choice != "back" and choice != "quit"):
            sys.exit("程序已退出")
        elif choice == "back":
            this_layer = this_layer_upper_levels
        elif choice == "quit":
            sys.exit("程序已退出")
        else:
            this_layer_upper_levels = this_layer
            this_layer = this_layer[choice]
    

      

posted @ 2018-04-14 20:11  等待の喵  阅读(460)  评论(1编辑  收藏  举报