标签:not 删除 打印 pre 爱奇艺 open art continue use
数据结构:
1 menu = { 2 ‘北京‘: { 3 ‘海淀‘: { 4 ‘五道口‘: { 5 ‘soho‘: {}, 6 ‘网易‘: {}, 7 ‘Google‘: {} 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 }
需求: 可依次选择进入各子菜单 可从任意一层往回退到上一层 可从任意一层退出程序 所需新知识点:列表、字典
low版:
不能返回上一级,直接返回最高层
1 #!/usr/bin/env python 2 # -*- coding=utf-8 -*- 3 menu = { 4 ‘北京‘: { 5 ‘海淀‘: { 6 ‘五道口‘: { 7 ‘soho‘: {}, 8 ‘网易‘: {}, 9 ‘Google‘: {} 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 exit_flag = False 47 while not exit_flag: 48 for i in menu: 49 print(i) # 列出可查询的省份 50 province = input("请输入您要查询的省份,或按Q退出:") # 输入需要查询的省 51 if province == ‘q‘ or province == ‘Q‘: # 判断输入的是否是退出 52 exit_flag = True 53 elif province in menu: 54 user_province = menu[province].keys() # 将字典中的所有键存入user_province中 55 for j in user_province: 56 print(j) 57 city = input("请输入需要查询的城市:") 58 if city in menu[province]: 59 user_city = menu[province][city].keys() 60 for k in user_city: 61 print(k) 62 country = input("请输入需要查询的区县:") 63 if country in menu[province][city]: 64 user_country = menu[province][city][country].keys() 65 for v in user_country: 66 print(v) 67 elif country == ‘q‘ or country == ‘Q‘: 68 exit_flag = True 69 elif country == ‘t‘ or country == ‘T‘: 70 continue 71 elif city == ‘q‘ or city == ‘Q‘: 72 exit_flag = True 73 elif city == ‘t‘ or city == ‘T‘: 74 continue
修改版:
1 # -*- coding=utf-8 -*- 2 """ 3 @author:Wllen 4 @file:dict_3.py 5 @time:2018/5/17 9:50 6 """ 7 menu = { 8 ‘北京‘: { 9 ‘海淀‘: { 10 ‘五道口‘: { 11 ‘soho‘: {}, 12 ‘网易‘: {}, 13 ‘Google‘: {} 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 list_menu = [] # 存放从字典中取得的值 51 while True: 52 if len(list_menu) == 0: # 判断list_menu中是否有值 53 user_menu = menu # 临时字典,用于存放从menu中取得的值 54 else: 55 user_menu = list_menu[-1] # 只要列表有值,取最后一个值 56 for i in user_menu: 57 print(i) # 打印列表中的倒数第一个值 58 user_input = input("请输入您要查询地点,返回上一级按T,退出按Q:").strip() # 输入需要查询的内容 59 if user_input == ‘q‘ or user_input == ‘Q‘: # 判断输入的是否是退出q 60 break 61 if user_input == ‘t‘ or user_input == ‘T‘: # 判断输入是否是返回上一级 62 if len(list_menu) == 0: 63 print(‘已经到顶了,没有上一级了‘) 64 continue 65 else: 66 list_menu.pop() # 当列表不为空时,删除最后一个值,下一个循环打印出来的就是上一级菜单的内容 67 continue 68 if len(list_menu) == 0: 69 list_menu.append(user_menu[user_input]) # 列表为空时,将第一次取得的值追加到列表中 70 else: 71 list_menu.append(list_menu[-1][user_input]) # 列表不为空时,将获取的值追加到列表的最后一位
Python开发入门14天集训营·第2章Python 数据类型、字符编码学习-3级菜单
标签:not 删除 打印 pre 爱奇艺 open art continue use
原文地址:https://www.cnblogs.com/mifengwei/p/9049966.html