python三级菜单实例(傻瓜版和进阶版)

程序: python三级菜单

要求: :
1.打印省、市、县三级菜单
2.可返回上一级
3.可随时退出程序

方案一:傻瓜版(其实傻瓜版考察的主要是思路!思路清楚了,那才不是傻瓜!O(∩_∩)O哈哈~)


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

while True:
    for i in menu:
        print(i)
    choice = input("请选择进入1>>:")  # 第一级

    if choice in menu:
        while True:
            for i2 in menu[choice]:
                print("	",i2)
            choice2 = input("请选择进入2>>:")    # 第二级

            if choice2 in menu[choice]:
                while True:
                    for i3 in menu[choice][choice2]:
                        print("		", i3)
                    choice3 = input("请选择进入3>>:")  # 第三级
                    
                    if choice3 in menu[choice][choice2]:
                        for i4 in menu[choice][choice2][choice3]:
                            print("			",i4)
                        choice4 = input("最后一层,按b返回>>:")  # 第四级,主要是为了返回上一层
                        if choice4 == "b":
                            pass
                    if choice3 == "b":
                        break
            if choice2 == "b":
                break

在这里插入图片描述

方案二:进阶版(虽说是进阶版,但其实改的地方只是代码重复的地方,思路没变,结果也一样)


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


exit_flag = False
current_layer = menu

layers = [menu]

while not  exit_flag:
    for k in current_layer:
        print(k)
    choice = input(">>:").strip()
    if choice == "b":
        current_layer = layers[-1]
        #print("change to laster", current_layer)
        layers.pop()
    elif choice not  in current_layer:continue
    else:
        layers.append(current_layer)
        current_layer = current_layer[choice]

在这里插入图片描述

本人目前在学习python、前端、数据库和linux相关的内容,故打算写一些学习笔记,包括安装软件遇到的一些问题、编程语言的学习。 学习如逆水行舟,你在原地踏步的同时,别人一直在前进!
原文地址:https://www.cnblogs.com/souhaite/p/10585614.html