zoukankan      html  css  js  c++  java
  • python基础--实现多级菜单

    代码效果比较:

    很low的代码版本展示如下

     1 menu ={
     2     "福建":{
     3         "福州":{
     4             "闽侯":{},
     5             "连江":{},
     6             "马尾":{}
     7         },
     8         "厦门": {
     9             "思明":{},
    10             "湖里": {},
    11             "集美": {}
    12         },
    13         "泉州": {
    14             "丰泽":{},
    15             "洛江": {},
    16             "泉港": {}
    17         },
    18 
    19     },
    20     "上海":{
    21         "浦东":{
    22             "陆家嘴":{},
    23             "张江":{},
    24             "川沙":{}
    25         },
    26         "静安": {
    27             "静安寺":{},
    28             "南京西路":{}
    29         },
    30         "宝山": {
    31             "吴淞街道":{},
    32             "友谊街道": {},
    33             "张庙街道": {}
    34         },
    35 
    36     },
    37     "广东":{
    38         "广州":{
    39             "白云区":{},
    40             "海珠区": {},
    41             "越秀区": {}
    42 
    43         },
    44         "深圳": {
    45             "南山区": {},
    46             "罗湖区": {},
    47             "宝安区": {}
    48         },
    49         "汕头": {
    50             "潮阳区": {},
    51             "潮安区": {},
    52             "澄海区": {}
    53         },
    54 
    55     }
    56 }
    57 
    58 back_flag = False
    59 exit_flag = False
    60 while not back_flag and not exit_flag:
    61     for key in menu:
    62         print(key)
    63     choice = input("请输入名称>>>: ").strip()
    64     if choice in menu:
    65         while not back_flag and not exit_flag:#让程序停留在第二层
    66             for key2 in menu[choice]:
    67                 print(key2)
    68             choice2 = input("第二次输入>>>: ").strip()
    69             if choice2 == 'b':
    70                 back_flag = True
    71             if choice2 in menu[choice]:
    72                  while not back_flag and not exit_flag:
    73                     for key3 in menu[choice][choice2]:
    74                         print(key3)
    75                     choice3 = input("第三次输入>>>: ").strip()
    76                     # if choice3 in  menu[choice][choice2][choice3]:
    77                     #     while not back_flag:
    78                     #         for key4 in menu[choice][choice2][choice3]:
    79                     #             print(key4)
    80                     print("没有更多了!")
    81                     if choice3 =='b':
    82                         back_flag = True
    83                     if choice3 =='q':
    84                         exit_flag = True
    85                  else:
    86                     back_flag = False
    87         else:
    88             back_flag = False

    更新后代码版本:

     1 menu ={
     2     "福建":{
     3         "福州":{
     4             "闽侯":{},
     5             "连江":{},
     6             "马尾":{}
     7         },
     8         "厦门": {
     9             "思明":{},
    10             "湖里": {},
    11             "集美": {}
    12         },
    13         "泉州": {
    14             "丰泽":{},
    15             "洛江": {},
    16             "泉港": {}
    17         },
    18 
    19     },
    20     "上海":{
    21         "浦东":{
    22             "陆家嘴":{},
    23             "张江":{},
    24             "川沙":{}
    25         },
    26         "静安": {
    27             "静安寺":{},
    28             "南京西路":{}
    29         },
    30         "宝山": {
    31             "吴淞街道":{},
    32             "友谊街道": {},
    33             "张庙街道": {}
    34         },
    35 
    36     },
    37     "广东":{
    38         "广州":{
    39             "白云区":{},
    40             "海珠区": {},
    41             "越秀区": {}
    42 
    43         },
    44         "深圳": {
    45             "南山区": {},
    46             "罗湖区": {},
    47             "宝安区": {}
    48         },
    49         "汕头": {
    50             "潮阳区": {},
    51             "潮安区": {},
    52             "澄海区": {}
    53         },
    54 
    55     }
    56 }
    57 
    58 
    59 current_layer = menu
    60 parent_layer =[] #新建空列表
    61 flags = False
    62 while not flags:
    63     for key in current_layer:
    64         print(key)
    65     choice = input("请输入相应的名称【退出请输入q,返回上一层请输入b】>>>: ").strip()
    66     if choice in current_layer:
    67         parent_layer.append(current_layer)#将结果存入到列表中
    68         current_layer = current_layer[choice] #改变至输入的结果
    69     elif choice == 'b':
    70         if parent_layer:#空白的列表的bool值:false
    71             current_layer = parent_layer.pop()
    72     elif choice == 'q':
    73         flags = True
    74     else:
    75         print("查无此人")

    亦或者是这样的:

     1 menu ={
     2     "福建":{
     3         "福州":{
     4             "闽侯":{},
     5             "连江":{},
     6             "马尾":{}
     7         },
     8         "厦门": {
     9             "思明":{},
    10             "湖里": {},
    11             "集美": {}
    12         },
    13         "泉州": {
    14             "丰泽":{},
    15             "洛江": {},
    16             "泉港": {}
    17         },
    18 
    19     },
    20     "上海":{
    21         "浦东":{
    22             "陆家嘴":{},
    23             "张江":{},
    24             "川沙":{}
    25         },
    26         "静安": {
    27             "静安寺":{},
    28             "南京西路":{}
    29         },
    30         "宝山": {
    31             "吴淞街道":{},
    32             "友谊街道": {},
    33             "张庙街道": {}
    34         },
    35 
    36     },
    37     "广东":{
    38         "广州":{
    39             "白云区":{},
    40             "海珠区": {},
    41             "越秀区": {}
    42 
    43         },
    44         "深圳": {
    45             "南山区": {},
    46             "罗湖区": {},
    47             "宝安区": {}
    48         },
    49         "汕头": {
    50             "潮阳区": {},
    51             "潮安区": {},
    52             "澄海区": {}
    53         },
    54 
    55     }
    56 }
    57 
    58 level = []#空列表
    59 while True:
    60     for key in menu:
    61         print(key)
    62     choice = input(">>>: ").strip()
    63     if choice == 'b':
    64         if len(level) == 0:break
    65         menu = level[-1]
    66         level.pop()
    67 
    68     elif choice in menu:
    69         level.append(menu)
    70         menu = menu[choice]
    71     else:
    72         continue
  • 相关阅读:
    javaBean的理解
    配置tomcat8数据源(采用局部数据源方式)
    windows下apache报os 10048错误
    Windows下Apache的下载安装启动停止
    java通过数据库连接池链接oracle
    java连接oracle数据库
    eclipse配置svn方法
    JAVA多线程中start方法与run方法区别
    java程序在没有java环境的电脑上执行的方法(关键词jar,exe)
    js监听不到组合键
  • 原文地址:https://www.cnblogs.com/ruichow/p/9437128.html
Copyright © 2011-2022 走看看