Day2 三级菜单

三级菜单
需求:用户进来时打印所有菜单,然后用户输入选择的菜单,如果列表中有那么将进入下一级菜单,如果没有那么将继续打印当前菜单,如果用户输入b那么将回退到上一级菜单,如果用户输入q则直接退出程序

#!/usr/bin/env python3

# -*- coding: utf-8 -*-


menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'sohu':{},
                '网易':{},
                'google':{},
            },
            '中关村':{
                '汽车之家':{},
                '爱奇艺':{},
                'yoku':{},
            },
            '上地':{
                '百度':{},
            }
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{}
            }
        },
        '朝阳':{},
        '东城':{},
        '西城':{},
    },
    '上海':{
        '闵行':{
            '人民广场':{
                '炸鸡':{}
            }
        },
        '闸北':{
            '火车站':{
                '携程':{}
            }
        },
        '浦东':{}
    },
    '河北':{
        '邯郸':{
            '曲周':{
                '娘娘寨':{}
            }
        }
    },
    '山东':{},
}



while True:
    for k in menu:
        print(k)
    choice = input(">:").strip()
    if len(choice) == 0:
        continue

    if choice in menu:
        while True:
            for k in menu[choice]:
                print(k)
            choice2 = input(">:").strip()
            if choice2 == 0:
                continue

            if choice2 in menu[choice]:
                while True:
                    for k in menu[choice][choice2]:
                        print(k)
                    choice3 = input(">:").strip()
                    if choice3 == 0:
                        continue

                    if choice3 in menu[choice][choice2]:
                        while True:
                            for k in menu[choice][choice2][choice3]:
                                print(k)
                            choice4 = input(">:").strip()
                            if choice4 == 0:
                                continue
                            elif choice4 == 'b':
                                break
                            elif choice4 == 'q':
                                exit("bye")
                    elif choice3 == 'b':
                        break
                    elif choice3 == 'q':
                        exit("bye")

            elif choice2 == 'b':
                break
            elif choice2 == 'q':
                exit("bye")

    elif choice == 'q':
        exit("bye")

 

文艺青年版(哈哈哈,下面这个厉害!!!)

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'sohu':{},
                '网易':{},
                'google':{},
            },
            '中关村':{
                '汽车之家':{},
                '爱奇艺':{},
                'yoku':{},
            },
            '上地':{
                '百度':{},
            }
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{}
            }
        },
        '朝阳':{},
        '东城':{},
        '西城':{},
    },
    '上海':{
        '闵行':{
            '人民广场':{
                '炸鸡':{}
            }
        },
        '闸北':{
            '火车站':{
                '携程':{}
            }
        },
        '浦东':{}
    },
    '河北':{
        '邯郸':{
            '曲周':{
                '娘娘寨':{}
            }
        }
    },
    '山东':{},
}

current_layer = menu
p_layer = []

while True:
    for k in current_layer:
        print(k)
    choice = input(">:").strip()
    if len(choice) == 0:
        continue

    if choice in current_layer:
        p_layer.append(current_layer)   #将当前目录存放到p_layer列表中
        current_layer = current_layer[choice]   #将当前目录重新赋值给current_layer
    elif choice == 'b':
        if len(p_layer) > 0:
            current_layer = p_layer.pop()   #将回退后的上一级菜单赋值给current_layer
    elif choice == 'q':
        exit("bye")

 

posted on 2018-03-06 09:04  李永山  阅读(152)  评论(0编辑  收藏  举报

导航