I would like to know how to use recursion in Python to do a function that traverses all oddities in a list and append them to a new list
Tested the code to get a null output. I ran the debugger and still could not find the reason. It stopped on line 14 (last line) and returned null, below the code is the output
My code so far:
def encontra_impares(lista):
lis = []
if len(lista) == 1 and lista[0] % 2 == 0:
if not lista[0] % 2 == 0:
return lis
return lista
else:
if lista[0] % 2 == 0:
return lista[0] + encontra_impares(lista[1:])
Output:
>>> encontra_impares([1,2,3])
[DEBUG ON]
>>>
Expected output:
>>> encontra_impares([1,2,3])
>>> '[1,3]'
Debugger:
>'_main_'.encontra_impares(), line 14 if lista[0] % 2 == 0: