• python程序: 三级菜单


    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    
    # 程序: 三级菜单
    #
    # 要求:
    #
    # 打印省、市、县三级菜单
    # 可返回上一级
    # 可随时退出程序
    
    from collections import Iterator
    menu = {
        '北京':{
            '海淀':{
                '五道口':{
                    'soho':{},
                    '网易':{},
                    'google':{}
                },
                '中关村':{
                    '爱奇艺':{},
                    '汽车之家':{},
                    'youku':{},
                },
                '上地':{
                    '百度':{},
                },
            },
            '昌平':{
                '沙河':{
                    '老男孩':{},
                    '北航':{},
                },
                '天通苑':{},
                '回龙观':{},
            },
            '朝阳':{},
            '东城':{},
        },
        '上海':{
            '闵行':{
                "人民广场":{
                    '炸鸡店':{}
                }
            },
            '闸北':{
                '火车战':{
                    '携程':{}
                }
            },
            '浦东':{},
        },
        '山东':{},
    }
    
    L=[] #存储当前状态
    node={} #存储当前节点地址
    
    for i in menu:
        print(i)
    node=menu
    L.append(node)
    
    #print(node)
    #print(isinstance(node,Iterator))
    #print("北京" in menu)
    
    while True:
    
    
        addr=str(input("输入地名:")).strip()
        if len(addr)==0:
            print("输出不能为空!请重输")
            continue
    
        if addr in node:   #判断地址是否存在于当前节点
            for i in node[addr]:
                print (i)
            L.append(node)
            node=node[addr]
        #    print(L)
        else:
            if addr=="q":
                print("欢迎下次再来!")
                break
    
            if addr == "b":
                if len(L)>1:
                    for i in L[-1]:
                        print(i)
                    L.pop(-1)
                    node=L[-1]
    
    
    
    
    

    三级菜单简化版

    menu = {
        '北京':{
            '海淀':{
                '五道口':{
                    'soho':{},
                    '网易':{},
                    'google':{}
                },
                '中关村':{
                    '爱奇艺':{},
                    '汽车之家':{},
                    'youku':{},
                },
                '上地':{
                    '百度':{},
                },
            },
            '昌平':{
                '沙河':{
                    '老男孩':{},
                    '北航':{},
                },
                '天通苑':{},
                '回龙观':{},
            },
            '朝阳':{},
            '东城':{},
        },
        '上海':{
            '闵行':{
                "人民广场":{
                    '炸鸡店':{}
                }
            },
            '闸北':{
                '火车战':{
                    '携程':{}
                }
            },
            '浦东':{},
        },
        '山东':{},
    }
    
    current_layer=menu #保存当前的状态
    last_layers=[menu]  #保存之前的访问状态
    
    
    while True:
        for i in current_layer:
            print(i)
    
        choice=input(">>:")
    
        if len(choice)==0:continue
    
        if choice in current_layer:
            last_layers.append(current_layer)
            current_layer=current_layer[choice]
    
    
        if choice=="b":
            if last_layers:
                current_layer=last_layers[-1]
                last_layers.pop(-1)
        if choice=="q":
            break
    
    
  • 相关阅读:
    268. Missing Number
    217. Contains Duplicate
    189. Rotate Array
    Two Sum II
    122. Best Time to Buy and Sell Stock II
    169. Majority Element
    C# ConfigurationManager不存在问题解决
    C# sqlhelper
    C#基础
    数据库事务日志已满的解决办法
  • 原文地址:https://www.cnblogs.com/yangzhenwei123/p/6758927.html
Copyright © 2020-2023  润新知