三级列表:

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

基础版本:

#首先要有个死循环,能够一直的输入
while True:
#打印第一层的数据
for key in menu:
print(key)
#然后叫用户选择
choice = input(">>:").strip()#去掉空格
#判断是否是回车
if choice == 'b':break
if choice == 'q':exit("欢迎下次再来")
if len(choice) ==0:continue#避免输入空格 print("------进入下一级------".center(50))
#第一,确保北京在字典里,有一个key,有,直接调这个key,进入下一级 #判断字典有没有这个值,有get和in两种方法
if choice not in menu:continue#如果不在里面继续往里输,跳过这次选择 #为下一层循环单独加一个循环
while True:
#先把下一层取到
for key2 in menu[choice]:
print(key2) choice2 = input(">>:").strip()
if choice2 == 'b': break
if choice2 == 'q': exit("欢迎下次再来{name}".format(name=choice2))
if len(choice2) ==0:continue
if choice2 not in menu[choice]:continue#判断用户输入的选项如果没在第这层菜单中,跳出 # 为下一层循环单独加一个循环
while True:
# 先把下一层取到
for key3 in menu[choice][choice2]:
print(key3) choice3 = input(">>:").strip()
if choice3 == 'b': break
if choice3 == 'q': exit("欢迎下次再来")
if len(choice3) == 0: continue
if choice3 not in menu[choice][choice2]: continue # 判断用户输入的选项如果没在第这层菜单中,跳出 while True:
# 先把下一层取到
for key4 in menu[choice][choice2][choice3]:
print(key4) choice4 = input(">>:").strip()
if choice4 == 'b': break
if choice4 == 'q': exit("欢迎下次再来")
if len(choice4) == 0: continue
if choice4 not in menu[choice][choice2][choice3]: continue # 判断用户输入的选项如果没在第这层菜单中,跳出

优化之后,高级版本:

current_level = menu #当前层 #初始变量
last_level = [] #定义空列表,返回上一级用的,每进入一层就存一层
while True:
for key in current_level:#循环每一层
print(key)
choice = input(">>:").strip()
if len(choice) == 0:continue # 返回上一层
# 自己记住每层的上一层,每次在进入下一层之前,我当前层就相当于下一层的父集
if choice == 'b':
# current_level = last_level#把当前层的变量改成上一层[把当前层改成父亲层,这样下一次循环就回到上一层]
if not last_level :break #if len(last_level) == 0:break
current_level = last_level[-1]#每退出一层取列表最后一个值
last_level.pop()#删一层
if choice not in current_level:continue
#choice在里面进入下一层
last_level.append(current_level)#每进一层存一层
current_level = current_level[choice]#给current_level重新赋值,进入下一层,每换一个变量,下一次就到了for key in current_level层

完毕。

python 三级菜单的更多相关文章

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

    程序: python三级菜单 要求: : 1.打印省.市.县三级菜单 2.可返回上一级 3.可随时退出程序 方案一:傻瓜版(其实傻瓜版考察的主要是思路!思路清楚了,那才不是傻瓜!O(∩_∩)O哈哈~) ...

  2. python(5)- 简单练习:python三级菜单优化

    python三级菜单优化,菜鸟版链接:http://www.cnblogs.com/xuyaping/p/6648170.html menu = { '北京':{ '海淀':{ '五道口':{ 'so ...

  3. python三级菜单的实现

    一.作业要求 1.使用字典实现三级菜单功能 2.直接输入前面数字进入下一级菜单 3.按B返回上一级,按Q退出 二.需要知识点 1.if循环 2.for循环,enumerate的用法 3.while循环 ...

  4. python 三级菜单 while循环三次,湖北省市-县-街道的选择,3个while的循环 -day2

    python编写一个三级while的循环菜单 1.定义字典,字典里面嵌套字典,内嵌字典的值为列表. 思路: 湖北省的市:字典中的定义3个字典,用于存储{序列-键:市名} shiqu_dir = {} ...

  5. Python 三级菜单 增强版

    需要实现的功能是:三级菜单1.从文本内读出选项2.查询每一级的选项,并能对选项进行增/删/改功能3.每一级可以退出程序或者返回上一层 2018-5-14 更新内容 思路 实现过程中的BUG及解决方案: ...

  6. python三级菜单

    #-*- coding:utf-8 -*-#Author:gxli #一级菜单项def menu(): #遍历字典dic一级菜单 print('-----------一级菜单------------- ...

  7. Python 三级菜单与优化(一层循环嵌套)

    优化的思路是使用单层循环嵌套完成三级菜单,这个优化思路我非常喜欢,我喜欢在编程的时候用最少的东西写出同样的效果,通常这样会绕来绕去,但非常有趣!!! 需求: 1.运行程序输出第一级菜单: 2.选择一级 ...

  8. Python——三级菜单

    #三级菜单函数 menu = { '北京':{ 海淀:{ '五道口':{} '中关村':{} '上帝':{} } '昌平':{} '朝阳':{} '东城':{} }, '上海':{} '山东':{} ...

  9. 4th,Python三级菜单

    1. 运行程序输出第一级菜单 2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单 3. 菜单数据保存在文件中 4. 让用户选择是否要退出 5. 有返回上一级菜单的功能 data = { '北京': ...

  10. [terry笔记]python三级菜单

    把三级菜单输出,选择后逐层显示,”b“返回上一级菜单. menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村': ...

随机推荐

  1. Apache Tomcat

    官网:http://tomcat.apache.org/ Documentation:http://tomcat.apache.org/tomcat-8.0-doc/index.html

  2. javascript中可变值与不可变值(原始值)

    字符串原始值修改不了1 var str = "abc"; 2 str[0] = "d"; 3 console.log(str[1]="f") ...

  3. KendoUI系列:Window

    1.基本使用 <link href="@Url.Content("~/Content/kendo/2014.1.318/kendo.common.min.css") ...

  4. maven+svn忽略提交到svn的文件

  5. Render OpenCascade Geometry Curves in OpenSceneGraph

    在OpenSceneGraph中绘制OpenCascade的曲线 Render OpenCascade Geometry Curves in OpenSceneGraph eryar@163.com ...

  6. 【踩坑经历】一次Asp.NET小网站部署踩坑和解决经历

    2013年给1个大学的小客户部署过一个小型的Asp.NET网站,非常小,用的sqlite数据库,今年人家说要换台服务器,要重新部署一下,好吧,虽然早就过了服务时间,但无奈谁叫人家是客户了,二话不说,上 ...

  7. Java多线程系列--“基础篇”08之 join()

    概要 本章,会对Thread中join()方法进行介绍.涉及到的内容包括:1. join()介绍2. join()源码分析(基于JDK1.7.0_40)3. join()示例 转载请注明出处:http ...

  8. php常见的面试题目

    一. 基本知识点1.1 HTTP协议中几个状态码的含义:503 500 401 403 404 200 301 302...200 : 请求成功,请求的数据随之返回.301 : 永久性重定向.302 ...

  9. 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误

    在visual studio 中添加数据库应用时,报错,提示如下: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL ...

  10. Android Fragment完全解析

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...