Python实现决策树的代码报错:UnboundLocalError: local variable 'classlabel' referenced before assignment

问题遇到的现象和发生背景

在实现决策树的时候遇到了一个卡很久的bug,代码是参考的CSDN上的,问题出现后也搜过,也有人遇到同样的问题,但苦于没有解决方法,在利用构建好的决策树给新样本分类函数部分,出现UnboundLocalError: local variable 'classlabel' referenced before assignment,用我自己的数据集就不行,但是用人家的就可以,该函数如下:

def classify(decision_tree,features,test_example):
    firstfeature=list(decision_tree.keys())[0]#根节点
    seconddict=decision_tree[firstfeature]#第一个分类属性的值
    firstfeature_index=features.index(firstfeature)
    for key in seconddict.keys():
        if test_example[firstfeature_index]==key:

            if type(seconddict[key]).__name__=='dict':
                classlabel=classify(seconddict[key],features,test_example)
            else:
                classlabel = 0
                classlabel=seconddict[key]
            break
    return classlabel

希望能够帮忙解决┭┮﹏┭┮