python学习笔记(25) 堆栈和递归实现三级菜单

时间:2021-11-23 14:31:15
menu = {
'北京':{
'海淀':{
'五道口':{},
'上地':{},
'西三旗':{}
},
'昌平':{},
'朝阳':{},
'东城':{}
},
'上海':{},
'山东':{}
}

def threeLM(dic):

  while True:

    for k in dic:print(k)

    key = input('请输入:').strip()

    if key == 'b' or key == 'q': return key

    elif key in dic.keys() and dic[key]:

      ret = threeLM(dic[key])

      if ret == 'q': return 'q'

    elif (not dic.get(key)) or (not dic[key]):

      continue

threeLM()

 

 

l = [menu]

while l:
for key in l[-1]:print(key)
k = input('input:').strip()
if k in l[-1].keys() and l[-1][k]:l.append(l[-1][k])
elif k == 'b':l.pop()
elif k == 'q':break
else:continue