Python 三级菜单实例

一、实现功能

  • 打印出选择显示内容
  • 按b返回上一级
  • 按q退出循环

二、代码实例

data = {
    '北京':{
        "昌平":{
            "沙河":['oldboy','test'],
            "天通苑":['我爱我家','链家地产']
        },
        '朝阳':{
            "望京":['望京soho'],
            "国贸":['中国尊'],
            "四惠":['长途汽车站']
        },
        '海淀':{
            '中关村':['北京大学','清华大学','101中学']
        }
    },
    '上海':{

    }
}

exit_flag = False
while not exit_flag:
    for l1 in data:
        print(l1)
    choice = input("请选择l1")
    if choice in data:
        while not exit_flag:
            for l2 in data[choice]:
                print('\t',l2)
            choice2 = input("请选择l2")
            if choice2 in data[choice]:
                while not exit_flag:
                    for l3 in data[choice][choice2]:
                        print('\t\t',l3)
                    choice3 = input("请选择l3")
                    if choice3 in data[choice][choice2]:
                        for l4 in data[choice][choice2][choice3]:
                            print('\t\t\t',l4)
                        choice4 = input('最后一层,按b退回')
                        if choice4 == 'b':
                            pass
                        elif choice4 == 'q':
                            exit_flag = True
                    if choice3 == 'b':
                        break
                    elif choice3 == 'q':
                        exit_flag = True
            if choice2 == 'b':
                break
            elif choice2 == 'q':
                exit_flag = True

  

猜你喜欢

转载自www.cnblogs.com/jiugongzi/p/9554685.html