三级菜单

要求:

升级作业:
程序: 三级菜单
要求: 
打印省、市、县三级菜单
可返回上一级
可随时退出程序

dic = {"河北":{"高碑店":["安新县","高阳县","涞水县"],"秦皇岛":["海港区","邯郸市","邢台市"]},
       "辽宁":{"沈阳":["沈北","大东","于洪","铁西"],"本溪":["平山区","明山区","溪湖区"]}
       }
flg=True
while flg:
    a = input("")
    if a.upper()=="Q":
        break
    else:
        if a =="河北":
            print("选择市")
            for x in dic["河北"]:
                print(x)
        elif a =="辽宁":
            print('选择市')
            for y in dic["辽宁"]:
                print(y)
        else:
            continue
        flg2 = True
        while flg2:
            b = input("")
            if b.upper( )=="Q":
                flg = False
                flg1 = False
                flg2 = False
                break
            elif b.upper()=="P":
                break
            else:
                if b=="高碑店":
                    print("选择县")
                    print(dic["河北"]["高碑店"])
                elif b == "秦皇岛":
                    print("选择县")
                    print(dic["河北"]["秦皇岛"])
                elif b == "沈阳":
                    print("选择县")
                    print(dic["辽宁"]["沈阳"])
                elif b == "本溪":
                    print("选择县")
                    print(dic["辽宁"]["本溪"])
                else:
                    print("重新输入")
                    continue
                flg1 = True
                while flg1:
                    c = input("")
                    if c.upper()=="Q" :
                        flg = False
                        flg1 = False
                        flg2 = False
                        break
                    elif c.upper()=="P":
                        break
                    else:
                        if c in dic["河北"]["高碑店"] or c in dic["河北"]["秦皇岛"] or c in dic["辽宁"]["沈阳"] or c in dic["辽宁"]["本溪"]:
                            print("%s省,%s市,%s县" %(a,b,c))
                            flg=False
                            flg1=False
                            flg2=False
                            break
                        else:
                            print("重新输入")
                            continue

用递归方法做

menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车战': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}

程序

 def threeLM(dic):
      while True:
          for k in dic:print(k)
          key = input('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'

 
 threeLM(menu)

 

posted @ 2018-03-06 18:01  wzqwer  阅读(183)  评论(0编辑  收藏  举报