首页 > 代码库 > 三级菜单

三级菜单

需求:

1、三级菜单

2、可依次选择进入各子菜单

3、可依次返回和终止各层次

流程图:

技术分享

 

程序如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Jack.Lee
# FileName: three_level_menu
#三级菜单采用字典来做,例子:省份—地市—景点
spot = {
    广东:{
        "广州":{"广州塔","农讲所","白云山"},
        "深圳":{"世界之窗","欢乐谷","锦绣中华"},
        "湛江":{"湖光岩","海滨公园","观海长廊"}
    },
    广西:{
        "南宁":{"青秀山","凤凰岭","龙虎山"},
        "桂林":{"阳朔","漓江","西街"},
        "北海":{"银滩","老街","涠洲岛"}
    },
    浙江:{
        "杭州":{"西湖","灵隐寺","千岛湖"},
        "温州":{"青龙湖","九龙潭","望海楼"},
        "宁波":{"东钱湖","天一阁","二灵塔"}
    }
}

exit_flag = False

while not exit_flag:
    for P in spot:     #打印第一层菜单
        print(P)
    province = input("请输入省份(按q退出):")
    if province == "q":
        print("您已退出系统,欢迎下次再用!")
        break
    if province in spot:
        while not exit_flag:    #进入二级菜单循环
            for C in spot[province]:
                print(C)
            city = input("请输入地市(按q退出,b返回上一级):")
            if city == "b":
                break
            elif city == "q":
                exit_flag = True
                print("您已退出系统,欢迎下次再用!")
            elif city in spot[province]:
                while not exit_flag:  # 进入三级菜单循环
                    for S in spot[province][city]:
                        print(S)
                    scenic_spot = input("请输入景区(按q退出,b返回上一级):")
                    if scenic_spot == "b":
                        break
                    elif scenic_spot == "q":
                        exit_flag = True
                        print("您已退出系统,欢迎下次再用!")
                    elif scenic_spot in spot[province][city]:
                        print("您选择的景区:",\033[34;1m,scenic_spot,\033[0m)             #30:黑,31:红 ,32:绿,33:黄 ,34:蓝色,35:紫色,36:深绿 ,37:白色;
                        exit_flag = True
                    else:
                         print("\033[31;0m对不起!你输入的景区不存在,请重新输入!\033[0m")  #打印错误信息,并且用红色标注。颜色标识方法:\033[显示方式;前景色;背景色m
            else:
                print("\033[31;0m对不起!你输入的地市不存在,请重新输入!\033[0m")
    else:
        print("\033[31;0m对不起!你输入的省份不存在,请重新输入!\033[0m")

 

三级菜单