1
I would like to know how to use Python recursion to make a function that traverses all the odd notes from a list and add them to a new list
Tested the code I got 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:])
Exit:
>>> encontra_impares([1,2,3])
[DEBUG ON]
>>>
Expected exit:
>>> encontra_impares([1,2,3])
>>> '[1,3]'
Debugger:
>'_main_'.encontra_impares(), line 14 if lista[0] % 2 == 0:
And what’s the problem with your code? You forgot to describe it in the question. Error? Which one? Did not fail but does not work? How did you test? What was the tested input? What was the output? What should be the output?
– Woss
I had tested, the output is null, IE does not result in anything this code
– l.martini
Exactly. Edit the question and add what test you did explaining that the output was null and put what the output should have been.
– Woss