zoukankan      html  css  js  c++  java
  • 简单练习:Python三级菜单优化

     1 menu = {
     2     '北京':{
     3         '海淀':{
     4             '五道口':{
     5                 'soho':{},
     6                 '网易':{},
     7                 'google':{}
     8             },
     9             '中关村':{
    10                 '爱奇艺':{},
    11                 '汽车之家':{},
    12                 'youku':{},
    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 }

    代码如下:

     1 current_layer=menu        #当前层
     2 last_layers=[menu]        #上一层
     3 while True:
     4     for key in current_layer:         #打印第一层菜单
     5         print(key)
     6     choice=input(">>:").strip()       #选择第二层菜单
     7     if choice in current_layer:            
     8         last_layers.append(current_layer)        #进入下一层菜单前,把当前层菜单加入上一次菜单中
     9         current_layer=current_layer[choice]      #当前层菜单被重新定义,进入循环打印下一层菜单
    10     if choice==0:                                #选择菜单层为空,结束本次循环
    11         continue
    12     if choice=="q":                             #选择菜单层为“q”,结束本层循环
    13         break
    14     if choice=="b":                             #选择菜单层为“b”,返回上一层菜单
    15         current_layer=last_layers[-1]           #返回上一层菜单前,当前层被重新定义
    16         last_layers.pop()                       #删除最后一次进入下一层菜单所加入的上一层列表数据
    17 print("程序结束!")
    18   
  • 相关阅读:
    docker 部署 nginx+php+mysql
    jquery-weui picker组件实现只选择年月
    ios端微信浏览器禁止上下滑动
    mysql 统计连续天数
    mysql 省市数据
    php 获取毫秒时间戳
    create-react-app 打包后文件路径问题
    php nginx 获取header信息
    ubuntu或者debian安装php-gd扩展错误
    php xml字符串转数组
  • 原文地址:https://www.cnblogs.com/sun1994/p/7999520.html
Copyright © 2011-2022 走看看