• 练习一个三级菜单


    首先按照自己的想法写出来

    menu = {
    '北京': {
    '朝阳': {
    '国贸': {
    'CCC': {},
    'HP': {},
    'CCTV': {},
    },
    },
    '昌平': {
    '昌平': {
    '天通苑': {},
    },
    },
    '海淀': {
    '默默': {
    '陌陌': {},
    },
    '五道口': {
    '快手': {},
    }
    },
    },
    '上海': {
    '浦东': {
    '陆家嘴': {
    '高盛': {},
    },
    '外滩': {},
    },
    '闵行': {},
    },
    '山东': {
    '济南': {},
    '德州': {
    '乐林': {
    '订看':{},
    },
    },
    },
    }
    break_flag = False
    exit_flag = False
    while not break_flag and not exit_flag :
    for key in menu:
    print(key)
    choice = input("1>>:").strip()
    if choice == 'b':
    break_flag = True
    if choice == 'q':
    exit_flag = True
    if choice in menu:
    while not break_flag and not exit_flag:
    for key2 in menu[choice]:
    print(key2)
    choice2 = input("2>>:").strip()
    if choice2 == 'b':
    break_flag = True
    if choice == 'q':
    exit_flag = True
    if choice2 in menu[choice]:
    while not break_flag and not exit_flag:
    for key3 in menu[choice][choice2]:
    print(key3)
    choice3 = input("3>>:").strip()
    if choice2 == 'b':
    break_flag = True
    if choice == 'q':
    exit_flag = True
    if choice3 in menu[choice][choice2]:
    for key4 in menu[choice][choice2][choice3]:
    print(key4)
    else:
    break_flag = False

    else:
    break_flag = False
    else:
    break_flag = False
    ==========================================
    如果上面的完成了!可以试试优化 因为看上去上面的代码很多地方都是重复的




    menu = {
    '北京': {
    '朝阳': {
    '国贸': {
    'CCC': {},
    'HP': {},
    'CCTV': {},
    },
    },
    '昌平': {
    '昌平': {
    '天通苑': {},
    },
    },
    '海淀': {
    '默默': {
    '陌陌': {},
    },
    '五道口': {
    '快手': {},
    }
    },
    },
    '上海': {
    '浦东': {
    '陆家嘴': {
    '高盛': {},
    },
    '外滩': {},
    },
    '闵行': {},
    },
    '山东': {
    '济南': {},
    '德州': {
    '乐林': {
    '订看':{},
    },
    },
    },
    }
    current_layer = menu
    parent_layers = []
    while True:
    for key in current_layer:
    print(key)
    choice = input(">>:").strip()
    if len(choice) == 0:continue
    elif choice in current_layer:
    parent_layers.append(current_layer)
    current_layer =current_layer[choice]
    elif choice == 'b':
    if parent_layers:
    current_layer = parent_layers.pop()
    else:
    print("input error!")


  • 相关阅读:
    母函数做的题
    HDU2089 暴力打表
    HDU2036 改革春风吹满地
    HDU1201 水题
    高可用服务 AHAS 在消息队列 MQ 削峰填谷场景下的应用
    Nacos Committers 团队首亮相,发布 0.9.0 版本
    Dubbo Mesh 在闲鱼生产环境中的落地实践
    Watchdogs利用Redis实施大规模挖矿,常见数据库蠕虫如何破?
    阿里在使用一种更灵活的软件集成发布模式
    2019 年,容器技术生态会发生些什么?
  • 原文地址:https://www.cnblogs.com/zoery/p/9214465.html
Copyright © 2020-2023  润新知