前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python学习之路之案例3--多级菜单

python学习之路之案例3--多级菜单

作者头像
py3study
发布2020-01-09 16:04:18
5020
发布2020-01-09 16:04:18
举报
文章被收录于专栏:python3python3

一、整个案例运用到的知识点

   1.python数据结构之字典的使用,字典嵌套字典,字典嵌套列表

   2.python数据结构之列表的使用,字典嵌套列表

   3.python数据结构之字符串的使用,字符串的格式化

   4.while True死循环的使用

   5.if...else....语句的使用

二、案例设计核心思想

    1.将城市信息存储在字典+列表的数据结构里面里面    

   2.将省、市、先设置成三级菜单

   3.按照用户的选择可依次选择进入各子菜单

三、代码

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
message_dict = {
    "四川":{"广安":["武胜","岳池","邻水"],"广元":["旺苍","苍溪","广元市区"]},
    "北京":{"海淀":["中关村","五道口","上地"],"朝阳":["常营","国贸","管庄"]}
}

print message_dict.keys()

"""
#使用字典和列表村村省、市、城镇等信息
message_dict = {
    "sichuan":{
        "guangan":["wusheng","yuechi","linshui"],
        "guangyuan":["wangcang","cangxi"]
    },
    "beijing":{
        "chaoyang":["changying","guanzhuang","guomao"],
        "haidian":["zhongguancun","wudaokou","shangdi"]
    }
}

#打印显示所有的省份
for i in range(len(message_dict.keys())):
    new_item1 = "%s:%s" %(i+1,message_dict.keys()[i])
    print new_item1

#要求用户选择要查看的的省份下面有哪些市区,并保证用户输入正确的省份
provence = raw_input("plz input select province:")
while True:
    if provence not in message_dict.keys():
        print "plz input correct!!!"
        provence = raw_input("plz input select province again:")
    else:
        break
message_dict1 = message_dict[provence]

#显示对应省份的所有市区的信息
for j in range(len(message_dict1)):
    new_item2 = "%s:%s" %(j+1,message_dict1.keys()[j])
    print new_item2


#要求用户输入要查看的市区的有哪些城镇,并保证用户输入正确
city = raw_input("plz select city:")
while True:
    if city not in message_dict1.keys():
        print "plz input correct!!!"
        city = raw_input("plz select city again:")
    else:
        break
message_list2 = message_dict1[city]
#打印用户所要查看的市区的的所有城镇
for k in range(len(message_list2)):
    new_item3 = "%s:%s" %(k+1,message_list2[k])
    print new_item3
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档