12/15 purify error


#1


Here is my code can someone tell me what is wrong?


#2

Change your function to build the list instead of removing from it,

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

#3

ohh i tried that way but i used to loop the items in the new_list instead in the lst thank you


#4

I have an identical code as yours zeziba but I am getting an error message saying;

Oops, try again. Your function crashed on [1] as input because your function throws a "'NoneType' object is not iterable" error.

My code is;

def purify(lst):
    new_list = []
    for a in lst:
        if a%2 == 0:
            new_list.append(a)
    print new_list

Can anyone see whats wrong with it?


#5

You new_list = [] has to go before your def purify(lst):

I did exactly the same thing :smile:


#6

Why is this only return [4] when input is [4,5,5,4], instead of returning [4,4] ?

def purify(x):
        new_list=[]
        for y in x:
            if y%2==0:
                new_list.append(y)
            return new_list

#7

I think your return statement should have the same indentation as for loop.


#8

You are right..> I tried deleting that post but it appears the post turned into a zombie and popped back up. :smile:


#9

That did the trick, appending it to the list instead of removing the odd numbers. Mucho gracias zeziba.


#10

Not print
It should be return in a function


#11

def purify(x):
lst = []
if len(x) == 1:
return []
else:
for i in x:
if i % 2 == 0:
lst.append(i)
return lst


#12

i have a same error TAT
my code is:
def purify(lists):

odd_numbers_list = []
for number in lists:

if number % 2 != 0:

odd_numbers_list.append(number)

>return odd_numbers_list

print odd_numbers_list
i was told " Your function fails on purify([1]). It returns [1] when it should return []." too.


#13

your
odd_numbers_list=[]
should be the first line in the for loop. So right under for number in lists:


#20

A post was split to a new topic: Help with error - your function fails on


#22