2 回答
TA贡献1835条经验 获得超7个赞
当您调用递归函数时,您缺少 a ,return因为这将是返回的函数True
def nestedListContains(lst, n):
for i in lst:
print(f'i: {i}, n: {n}')
if type(i) == list:
return nestedListContains(i, n)
elif int(i) == int(n):
return True
return False
print(nestedListContains([1, [2, [3], 4]], 3)) # True
print(nestedListContains([1, [2, [3], 4]], 5)) # False
TA贡献1805条经验 获得超10个赞
您需要返回调用的值nestedListContains(i, n),否则返回的值将被丢弃,并且循环将继续,直到return False到达:
def nestedListContains(lst, n):
for i in lst:
print(f'i: {i}, n: {n}')
if type(i) == list:
return nestedListContains(i, n)
elif int(i) == int(n):
return True
return False
添加回答
举报