Purifies but not completely!


#1

Why does this code removes only some odd numbers?

Oops, try again. Your function fails on purify([4, 5, 5, 4]). It returns [4, 5, 4] when it should return [4, 4].

Thank you.
DD


#2

I think remove might not be the best suitable solution to this problem, Why? remove does it fine when you would call:

purify([1,2,3,4])

but the moment you have a duplicate values (two times 1, two times 3, two times 5) it seems it only removes one, so either you figure out why this is, try a different approach (maybe define an list in the start of your function, and put even numbers into the list)?


#3

Thank you stetim94.
I'm going to using a list and will let you know how I get on. Thanks again
DD


#4

I tried creating a list and avoiding remove() but the results are equally disappointing:

Oops, try again. Your function fails on purify([1]). It returns [2, 4, 6, 8] when it should return [].

I completely fail to figure out why, when a list with just a 1 is passed to the function, a bunch of even numbers creep in. From where?!!!

Thank you,
DD


#5

weird, that shouldn't happen. Can you copy your code the forum so i can run it?

Why do you have and i !=1? if i where 1, i % 2 == 0 would be false, 1 would net get append to the list


#6

Thank you steim94.
I have been suspecting than either my browser introduces bugs or I have a corrupted program or something....! I looked in a few manuals and it should have worked as well with the remove() method, but strange numbers where cropping up although it wasn't so clear.

Following I print the code although I found out that if I declare the temporal list inside the function thenthere is no extra number creeping in and it works. as in the picture.

----temp_list = []
def purify(cleanses):
----for i in cleanses:
--------if i % 2 == 0 and i !=1:
------------temp_list.append(i)
--------return temp_list

Many thanks again. Since you and a few other guys there are giving invaluable advice, I've began to finish some of the exercises without having to look at hints or solutions!


#7

This is weird, here is your code:

temp_list = []
def purify(cleanses):
    for i in cleanses:
        if i % 2 == 0 and i !=1:
            temp_list.append(i)
    return temp_list
print purify([4,5,5,4])

i only added a function call with a print statement so you could test different inputs, and it seems fine, it could be that your browser sends cached data, try a different browser. Nice, that is not only us, but also you getting better at coding, keep it going!


#8

You are very generous.
A gentleman and a Scholar!


#9

Did you manage to pass the exercise? Thank you :smile:


#10

Yes stetim94, I did!