Purify exercise Your function fails on purify([4, 5, 5, 4]). It returns [4] when it should return [4, 4]


#1

I attempted myself first and was actaully surprised i got pretty close a few exercises i’m just lost so i get the code to learn from it and double check:

def purify(lst):
evenlist = []
for o in lst:
if o % 2 == 0:
evenlist.append(o)
return evenlist

It had different variables but pretty much the same as the real answer so curious what was different

def purify(lst):
res = []
for ele in lst:
if ele % 2 == 0:
res.append(ele)
return res

That’s the answer it gives me. But mine gives me error: Your function fails on purify([4, 5, 5, 4]). It returns [4] when it should return [4, 4].


#2

By default, a function returns None at the end of the function

if we want to return something different at the end of the function, we can use the return keyword

Thus, the function ends when return keyword is reached

so your function ends in the first iteration of the loop given you nested return inside the loop


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.