美文网首页
015_三级菜单练习

015_三级菜单练习

作者: Nzkalhbxx | 来源:发表于2017-10-16 17:15 被阅读0次
    # __author__:Nzkalhbxx
    # __date__:2017/10/15
    menu = {
        '河北': {
            '石家庄': {
                '平山县': {},
                '新乐市': {},
                '辛集市': {}
            },
            '唐山': {
                '路南区': {},
                '开平区': {},
                '丰南区': {}
            },
            '邯郸': {
                '丛台区': {},
                '复兴区': {},
                '大名县': {}
            }
        },
        '湖北': {
            '武汉': {
                '江岸区': {},
                '江汉区': {},
                '武昌区': {}
            },
            '黄石': {
                '下陆区': {},
                '铁山区': {},
                '黄石港区': {}
            },
            '咸宁': {
                '嘉鱼县': {},
                '通城县': {},
                '崇阳县': {}
            }
        },
        '广东': {
            '广州': {
                '荔湾区': {},
                '天河区': {},
                '萝岗区': {}
            },
            '韶关': {
                '武江区': {},
                '曲江区': {},
                '浈江区': {}
            },
            '汕尾': {
                '陆河县': {},
                '陆丰市': {},
                '海丰县': {}
            }
        }
    }
    # loop_print_flag = True  # loop_print_flag用于标志是否循环输出当前等级菜单内的所有选项
    # notBack_flag = True     # notBack_flag用于标志是否不返回上一层
    # while loop_print_flag and notBack_flag:
    #     for ele in menu:
    #         print(ele)
    #     province = input("选择地点>>>:").strip()
    #     if province == "back":
    #         notBack_flag = False    # 当输入为back时, 将notBack_flag设置为False
    #     elif province in menu:
    #         while loop_print_flag and notBack_flag:
    #             for ele in menu[province]:
    #                 print(ele)
    #             city = input("选择地点>>>:").strip()
    #             if city == "back":
    #                 notBack_flag = False
    #             elif city in menu[province]:
    #                 while loop_print_flag and notBack_flag:
    #                     for ele in menu[province][city]:
    #                         print(ele)
    #                     county = input("选择地点>>>:").strip()
    #                     if county == "back":
    #                         notBack_flag = False
    #                     elif county in menu[province][city]:
    #                         print(''.join(["-->", county]))
    #                         lastChoice = input("是否选择完成:退出(quit)/回退(back):")
    #                         if lastChoice == "quit":
    #                              loop_print_flag = False
    #                              notBack_flag = False
    #                         elif lastChoice == "back":
    #                             notBack_flag = False
    #                 notBack_flag = True
    #             else:
    #                 loop_print_flag = True
    #         notBack_flag = True
    #     else:
    #         loop_print_flag = True
    # print("-------->退出选择<--------")
    
    
    # 精简版
    # loop_print_flag用于标志是否循环输出当前等级子菜单内的所有选项
    loop_print_flag = True
    # current_level用于保存当前循环所在菜单
    current_level = menu
    # 用于返回时使用, 每进入下一层时便保存上一级的目录菜单
    parent_level = []
    # 判断是否输入有误
    errorInput = True
    while loop_print_flag or errorInput:
        for ele in current_level:
            print(ele)
        choice = input("place is not exist, choose quit or back:>>>:").strip()
        if choice in current_level:
            parent_level.append(current_level)
            current_level = current_level[choice]
        elif choice == "back":
            if parent_level.__len__() > 0:
                current_level = parent_level.pop()
        elif choice == "quit":
            loop_print_flag = False
            errorInput = False
    print("-------->退出选择<--------")
    
    运行结果

    相关文章

      网友评论

          本文标题:015_三级菜单练习

          本文链接:https://www.haomeiwen.com/subject/fecluxtx.html