Python学习日记-12-homework-2-三级菜单

初级版本。

menu = {
    '北京':{
        '朝阳':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
        '海淀':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
    },
    '武汉':{
        '朝阳':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
        '海淀':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
    },
    '上海':{
        '朝阳':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
        '海淀':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
    },
}

flag_back = True
flag_exit = True

print('输入q离开,输入b返回上一层')

while flag_exit:
    for key_1 in menu:
        print(key_1)
    choice_1 = input('请输入您要选择的地方(1):').strip()
    if choice_1 in menu:
        while flag_exit:
            for key_2 in menu[choice_1]:
                print(key_2)
            choice_2 = input('请输入您要选择的地方(2):').strip()
            if choice_2 in menu[choice_1]:
                while flag_exit:
                    for key_3 in menu[choice_1][choice_2]:
                        print(key_3)
                    choice_3 = input('请输入您要选择的地方(3):').strip()
                    if choice_3 in menu[choice_1][choice_2]:
                        for key_4 in menu[choice_1][choice_2][choice_3]:
                            print(key_4)
                        choice_4 = input('请输入您要选择的地方(3):').strip()
                        if choice_4 == 'q' or choice_4 == 'Q':
                            flag_exit = False
                        elif choice_4 == 'b' or choice_4 == 'B':
                            break
                        else:
                            print('输入所错误,请重新输入!!!')
                    elif choice_3 == 'q' or choice_3 == 'Q':
                        flag_exit = False
                    elif choice_3 == 'b' or choice_3 == 'B':
                        break
                    else:
                        print('输入所错误,请重新输入!!!')
            elif choice_2 == 'q' or choice_2 == 'Q':
                flag_exit = False
            elif choice_2 == 'b' or choice_2 == 'B':
                break
            else:
                print('输入所错误,请重新输入!!!')
    elif choice_1 == 'q' or choice_1 == 'Q':
        flag_exit = False
    else:
        print('输入所错误,请重新输入!!!')

print('结束')

高级版本

menu = {
    '北京':{
        '朝阳':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
        '海淀':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
    },
    '武汉':{
        '朝阳':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
        '海淀':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
    },
    '上海':{
        '朝阳':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
        '海淀':{
            '沙河':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '回龙观':{
                '老男孩':{},
                '阿泰包子':{},
            },
            '紫荆城':{
                '老男孩': {},
                '阿泰包子': {},
            },
        },
    },
}

current_menu = menu     #实现动态循环
parent_list = []        #保存所有,最后一个元素永远是父亲级

while True:
    for key in current_menu:
        print(key)
    choice = input('请输入您要选择的地方:').strip()
    if len(choice) == 0:continue
    if choice in current_menu:
        parent_list.append(current_menu)     #进入下一层时,把当前层(下一级的父亲层)
        current_menu = current_menu[choice]   #改为子层
    elif choice == 'b' or choice == 'B':
        if parent_list:
            current_menu = parent_list.pop()  #弹出最后一个元素,即当前层的父亲层
    elif choice == 'q' or choice == 'Q':
        break
    else:
        print('无此项')

你可能感兴趣的:(Python学习日记-12-homework-2-三级菜单)