无法从递归函数返回值

2024-10-01 22:34:36 发布

您现在位置:Python中文网/ 问答频道 /正文

当试图通过忽略层次结构来获取嵌套字典中键的值时,它总是返回None

def nested(d,key):
    for i in d.keys():
        if i == key:
            return d[i]
        elif isinstance(d[i], dict):
            nested(d[i],key)

j = {'hello': {'foo': {'bar': {'world':'yeay'}}}}
print(nested(j,'world'))

当然,预期的答案是yeay,但我遗漏了什么


Tags: keyinnoneforworldreturnif字典

热门问题