字典循环训练

基础篇

# -*- coding:utf-8 -*-
data = {
    '浙江':{
        '杭州':{
            "西湖":['tea','silk'],
            "上城区":['链家','我爱我家']
        },
        '宁波':{
            "慈溪":['奔驰','默默'],
            "宁海":['SCI','HP'],
            "奉化":['Advent','Jewels']
        }
    },
    '广东':{
        "东莞":{},
        "常熟":{},
        "佛山":{}
    },
}

Exit_flag = False
while not Exit_flag:
    for Layer1 in data:
        print(Layer1)
    choice = input("Enter>>1:")
    if choice in data:
        while not Exit_flag:
            for Layer2 in data[choice]:
                print('\t',Layer2)
            choice2 = input("Enter>>2:")
            if choice2 in data[choice]:
                while not Exit_flag:
                    for Layer3 in data[choice][choice2]:
                        print('\t\t', Layer3)
                    choice3 = input("Enter>>3:")
                    if choice3 in data[choice][choice2]:
                        for Layer4 in data[choice][choice2][choice3]:
                            print('\t\t\t',Layer4)
                        choice4 = input("The last Layer,press 'b' to back>>:" )
                        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

    elif choice == 'q':
        Exit_flag = True

改进篇

'''
@author: XT
'''
# -*- coding:utf-8 -*-
data = {
    '浙江':{
        '杭州':{
            "西湖":['tea','silk'],
            "上城区":['链家','我爱我家']
        },
        '宁波':{
            "慈溪":['奔驰','默默'],
            "宁海":['SCI','HP'],
            "奉化":['Advent','Jewels']
        }
    },
    '广东':{
        "东莞":{},
        "常熟":{},
        "佛山":{}
    },
}

def init_first_layer():
    for Layer in data:
        print(Layer)
    i = 1
    choice = input("Enter>>{}:".format(i))
    choice = choice.strip()
    tabString_enter = '\t'
    choice_enter(choice, data, i, tabString_enter, exit_flag=False)

def choice_enter(choice, c_data, i, tabString_enter, exit_flag):
    while not exit_flag:
        if choice in c_data:
            for Layer in c_data[choice]:
                print(tabString_enter, Layer)
            while not exit_flag:
                put_data = c_data[choice]
                tabString_enter += '\t'
                end = 4
                i += 1
                while i < end:
                    choice = input("Enter>>{}:".format(i))
                    choice = choice.strip()
                    if choice in put_data:
                        choice_enter(choice, put_data, i, tabString_enter, exit_flag=False)
                    elif choice == 'b':
                        init_first_layer()
                    elif choice == 'q':
                        exit_flag = True
                        exit()
                    else:
                        print('Please input again!')
                        choice = input("Enter again>>{}:".format(i))
                        choice = choice.strip()
                else:
                    print('The last one! Back!')
                    choice = input("Enter again>>{}:".format(i))
                    choice = choice.strip()
                    if choice == 'q':
                        exit()
                    break
        elif choice == 'b':
            init_first_layer()
        elif choice == 'q':
            exit_flag = True
        else:
            print('Please input again!')
            choice = input("Enter again>>{}:".format(i))
            choice = choice.strip()



init_first_layer()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何以问天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值