Fork me on GitHub

三级菜单

作业需求

数据结构:

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

需求:
可依次选择进入各子菜单
可从任意一层往回退到上一层
可从任意一层退出程序
所需新知识点:列表、字典

代码:

# -*- coding:utf-8 -*-
#Author:Kris
menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '网易':{},
                'google':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{},
            },
            '上地':{
                '百度':{},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{},
            },
            '天通苑':{},
            '回龙观':{},
        },
        '朝阳':{},
        '东城':{},
    },
    '上海':{
        '闵行':{
            "人民广场":{
                '炸鸡店':{}
            }
        },
        '闸北':{
            '火车战':{
                '携程':{}
            }
        },
        '浦东':{},
    },
    '山东':{},
}
while True:
    for i in menu:
        print(i)   #打印省或直辖市
    choice = input("请输入省份或直辖市(退出请按q)-->>>:")
    if choice in menu:
        while True:
            for i2 in menu[choice]:
                print(i2)  #打印区县
            choice2 = input("请输入区县((返回上一级请按b,退出请按q))-->>:")
            if choice2 in menu[choice]:
                while True:
                    for i3 in menu[choice][choice2]:
                        print(i3)  #打印街道
                    choice3 = input("请输入街道-->>((返回上一级请按b,退出请按q))")
                    if choice3 in menu[choice][choice2]:
                        while True:
                            for i4 in menu[choice][choice2][choice3]:
                                print(i4)
                            choice_l = input("已经达到最后一级(返回上一级请按b,退出请按q)")
                            if choice_l == "b":
                                break
                            elif choice_l == "q":
                                exit()
                    elif choice3 =="b":  #从街道返回区县
                        break
                    elif choice3 =="q":
                        exit()
            elif choice2 == "b":  #从区县返回省或直辖市
                break
            elif choice2 =="q":
                exit()
    elif choice == "q":
        exit()

 

posted @ 2018-04-21 15:26  kris12  阅读(201)  评论(0编辑  收藏  举报
levels of contents