Python字典实现简单的三级菜单(实例讲解)
(编辑:jimmy 日期: 2024/11/20 浏览:3 次 )
如下所示:
data = { "北京":{ "昌平":{"沙河":["oldboy","test"],"天通苑":["链接地产","我爱我家"]}, "朝阳":{"望京":["奔驰","陌陌"],"国贸":["CICC","HP"],"东直门":["advent","飞信"]}, "海淀":{} }, "山东":{ "德州":{}, "青岛":{}, "济南":{} }, "广东":{ "东莞":{}, "常熟":{}, "佛山":{} } } exit_flag = False while not exit_flag: for i in data: print(i) choice_s = input("请输入以上任一省或直辖市名称>:") if choice_s in data: while not exit_flag: for i in data[choice_s]: print("\t",i) choice_q = input("请输入以上任一市区名称>:") if choice_q in data[choice_s]: while not exit_flag: for i in data[choice_s][choice_q]: print("\t\t",i) choice_j = input("请输入以上任一街道名称>:") if choice_j in data[choice_s][choice_q]: for i in data[choice_s][choice_q][choice_j]: print("\t\t",i) last = input("最后一层,按b返回>:") if last == "b": pass elif last == "q": exit_flag = True if choice_j == "b": break elif choice_j == "q": exit_flag = True if choice_q == "b": break elif choice_q == "q": exit_flag = True
要点:
1、字典的层级关系
2、break是终止本次循环,pass是什么也不做,一个占位符而已
3、巧妙使用exit_flag = False
以上这篇Python字典实现简单的三级菜单(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
下一篇:使用Kivy将python程序打包为apk文件