python学习之三级菜单

这次的作业:python之三级菜单。

数据结构

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

需求:

  • 可依次选择进入各子菜单
  • 可从任意一层往回退到上一层
  • 可从任意一层退出程序

代码实现

#!/usr/bin/env python
#_*_ coding:utf-8 _*_
__author__ = 'shunzi'

filename = 'menu.txt';
list_menu = []
with open(filename,'r',encoding="utf-8") as f:
    menu = eval(f.read())
while True:
    for i in menu:
        print('\t'*len(list_menu)+i)
    choice = input("请选择进入子菜单,按q退出,按b返回上一层菜单>>").strip()
    if choice == "q":
        break
    elif choice == "b":
        if len(list_menu) == 0:
            print("已经是最上一层菜单")
            continue
        menu = list_menu[-1]
        list_menu.pop()
        continue
    elif len(choice) == 0 or choice not in menu:
        print("你的输入有误,请重新输入>>")
        continue
    list_menu.append(menu)
    menu = menu[choice]

小结

本节主要考察了对列表的添加删除,字典的运用,现在看来超级简单,还有一种通过递归函数来实现的,有机会补上!

继续阅读
广告也精彩
shunzi
  • 本文由 发表于 2018-03-0816:06:30
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
deepin linux 15.11系统安装 BLOG

deepin linux 15.11系统安装

Deepin 15.11系统安装 window系统实在是太难用了,用了那么多linux系统,发现deepin系统真的是太好用了!特别推荐一下!!! 首先下载deepin系统ISO镜像,https://...
Python中字典的value是列表的运用 BLOG

Python中字典的value是列表的运用

今天群里一个朋友问了一个python问题,将列表a里的内容 转换成列表b那种格式,示例如下 a= 转换成: b= 简单来说 就是统计相同schema跟table的内容合并到一个list里面 一开始一直...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: