Python 字典应用

三级菜单:

低配版三级菜单:

dictionary = {
  '1': {
        'a': {
            'A1': 'hello world!',
            'A2': 'Hello world!',
            'A3': 'Hello World!'
        },
        'b': {
            'B1': 'better world',
            'B2': 'Better world',
            'B3': 'Better World'
        },
        'c': {
            'C1': 'cool world',
            'C2': 'Cool world', 
            'C3': 'Cool World'
        }
    },
    '2': {
        'd': {
            'Da': 'data date',
            'Db': 'Data date',
            'Dc': 'Data Date'
        },
        'e': {
            'Ea': 'elective word',
            'Eb': 'Elective word',
            'Ec': 'Elective Word'
        },
        'f': {
            'Fa': 'food foot',
            'Fb': 'Food foot',
            'Fc': 'Food Foot'
        }
    },
    '3': {
        'g': {
            'G': 'gg'
        },
        'h': {
            'H': 'hh'
        },
        'i': {
            'I': 'ii'
        }
    },
    '4': {
        'j': {
            'J': 'jj'
        },
        'k': {
            'K': 'kk'
        },
        'l': {}
    },
    '5': {
        'm': {},
        'n': {},
        'o': {}
    }
}

while True :
    for i in dictionary.keys():
        print(i, end=' ')  # 一级
    print(' ')
    menu1 = input("Please choose the menu: ")
    if menu1 == 'exit': 
        break
    if menu1 not in dictionary.keys():
        print("None such menu!")
        continue    
    for i in dictionary.keys():
        if menu1 == i:
            while True:
                for j in dictionary.get(i).keys():
                    print(j, end=' ')  # 二级
                print('')
                menu2 = input("Please choose the menu:")
                if menu2 == 'back':
                    break
                if menu2 not in dictionary.get(i).keys():
                    print("None such menu!")
                    continue 
                for j in dictionary.get(i).keys():
                    if menu2 == j:
                        while True:
                            for k in dictionary.get(i).get(j).keys():
                                print(k, end=' ')  # 三级
                            print('')
                            menu3 = input("Please choose the menu: ")
                            if menu3 == 'back':
                                break
                            if menu3 not in dictionary.get(i).get(j).keys():
                                print("None such menu!")
                                continue 
                            for l in dictionary.get(i).get(j).keys():
                                if menu3 == l:
                                    print(dictionary.get(i).get(j)[l])

高配版多级菜单:

dictionary = {
    '1': {
        'a': {
            'A1': 'hello world!',
            'A2': 'Hello world!',
            'A3': 'Hello World!'
        },
        'b': {
            'B1': 'better world',
            'B2': 'Better world',
            'B3': 'Better World'
        },
        'c': {
            'C1': 'cool world',
            'C2': 'Cool world', 
            'C3': 'Cool World'
        }
    },
    '2': {
        'd': {
            'Da': 'data date',
            'Db': 'Data date',
            'Dc': 'Data Date'
        },
        'e': {
            'Ea': 'elective word',
            'Eb': 'Elective word',
            'Ec': 'Elective Word'
        },
        'f': {
            'Fa': 'food foot',
            'Fb': 'Food foot',
            'Fc': 'Food Foot'
        }
    },
    '3': {
        'g': {
            'G': 'gg'
        },
        'h': {
            'H': 'hh'
        },
        'i': {
            'I': 'ii'
        }
    },
    '4': {
        'j': {
            'J': 'jj'
        },
        'k': {
            'K': 'kk'
        },
        'l': {}
    },
    '5': {
        'm': {},
        'n': {},
        'o': {}
    }
}

current_layer = dictionary  #当前菜单
parent_layer = []  #存储字典的上一级

while True:
    for key in current_layer:
        print(key, end=' ')
    print('')
    menu = input(">>>")
    if len(menu)==0: continue
    if menu == 'exit': break
    if menu in current_layer:       
        if type(current_layer[menu]) == type(current_layer):  #判断是否还有菜单
            parent_layer.append(current_layer)  #将父节点存入列表
            current_layer = current_layer[menu]  #当前菜单转为子菜单
        else:
            print(current_layer[menu])  #没有菜单则将值输出
    elif menu == 'back':
        if parent_layer:
            current_layer = parent_layer.pop()  #保证列表的最后一个元素都是当前菜单的上一级
    else:
        print("查无此项")
posted @ 2018-03-07 08:51  Clingyu  阅读(143)  评论(0)    收藏  举报