2 回答

TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個(gè)贊
當(dāng)您調(diào)用遞歸函數(shù)時(shí),您缺少 a ,return因?yàn)檫@將是返回的函數(shù)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貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊
您需要返回調(diào)用的值nestedListContains(i, n),否則返回的值將被丟棄,并且循環(huán)將繼續(xù),直到return False到達(dá):
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
添加回答
舉報(bào)