网络编程
位置:首页>> 网络编程>> Python编程>> Python字典实现简单的 * 菜单(实例讲解)

Python字典实现简单的 * 菜单(实例讲解)

作者:jingxian  发布时间:2021-07-13 12:03:32 

标签:python, , 菜单,字典

如下所示:


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

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com