python全栈笔记-day03-chapter2 homework1_menu

# #多级菜单
# 需求:
# 现有省、市、县3级结构,要求程序启动后,允许用户可依次选择进入各子菜单
# 可在任意一级菜单返回上一级
# 可在任意一级菜单退出程序
# 所需知识点:列表、字典

location = {
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '网易':{},
                'google':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{},
            },
            '上地':{
                '百度':{},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{},
            },
            '天通苑':{},
            '回龙观':{},
        },
        '朝阳':{},
        '东城':{},
    },
    '上海':{
        '闵行':{
            "人民广场":{
                '炸鸡店':{}
            }
        },
        '闸北':{
            '火车战':{
                '携程':{}
            }
        },
        '浦东':{},
    },
    '山东':{},
}

current_layer = location#记录当前层
layers = [ ]#记录进入过的层

while True:
    for k in current_layer:
        print(k)
    choice = input('>>')
    if not choice: continue
    if choice in current_layer:
        if len(current_layer[choice]) != 0:#还有下一层
            layers.append(current_layer)
            current_layer = current_layer[choice]#进入下一层
        else:
            print('您已在底层')
    elif choice == 'b':
        if len(layers) != 0:
            current_layer = layers.pop()
        else:
            print('您已在首层')
    elif choice == 'q':
        print('bye')
        break
    else: print('不存在')



#非常多重复代码
# exit_flag1 = False#标志位1
# while not exit_flag1:#选择省级
#     for k in location:  # 输出省级menu
#         print(k)
#     choice1 = input('请输入(b:返回上一级,q:退出):')
#     if not choice1: continue#如果输入回车(没输入),则继续输入
#     elif choice1 == 'q':#判断输入是否为q
#         exit_flag1 = True
#     elif choice1 == 'b':
#         print('您已在第一级')
#     else:
#         exit_flag2 = False  # 标志位2
#         if choice1 in location:#如果该名称存在
#             while not exit_flag2:#选择市级
#                 for i in location[choice1]:  # 输出市级menu
#                     print(i)
#                 choice2 = input('请输入(b:返回上一级,q:退出):')
#                 if not choice2: continue#如果输入回车(没输入),则继续输入
#                 elif choice2 == 'q':  # 判断输入是否为q
#                     exit_flag1 = True
#                     exit_flag2 = True
#                 elif choice2 == 'b':
#                     exit_flag2 =True
#                 else:
#                     exit_flag3 = False  # 标志位3
#                     if choice2 in location[choice1]:#如果该名称存在
#                         while not exit_flag3:
#                             for n in location[choice1][choice2]:  # 输出县级menu
#                                 print(n)
#                             choice3 = input('请输入(b:返回上一级,q:退出):')
#                             if choice3 == 'q':  # 判断输入是否为q
#                                 exit_flag1 = True
#                                 exit_flag2 = True
#                                 exit_flag3 = True
#                             elif choice3 == 'b':
#                                 exit_flag3 = True
#                             else:
#                                 if choice3 in location[choice1][choice2]:
#                                     print('您已选择',choice1,choice2,choice3)
#                                     exit_flag1 = True
#                                     exit_flag2 = True
#                                     exit_flag3 = True
#                                 else:
#                                     print('该名称不存在')
#                                     continue
#                     else:
#                        print('该名称不存在')
#                        continue
#         else:
#             print('该名称不存在')
#             continue

猜你喜欢

转载自blog.csdn.net/weixin_37267713/article/details/82826903
今日推荐