Purify seems to work but still returns error?


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/2?curriculum_id=4f89dab3d788890003000096#

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


I'm receiving this error message, despite the fact that the console is printing what looks to me like the correct response regardless of what input I give it. Can anyone help diagnose my issue?


numbers2 = []

def purify(numbers):
    for l in numbers:
        if l % 2 == 0:
            numbers2.append(l)
    return numbers2
                  
print purify([2,3,4,6])


#2

remove the last line


#3

That didn't seem to work.


#4

weird probably a bug maybe?


#5

I think you should put the numbers2 list inside your purify function, it worked for me that way

def purify(nums):
    ret = []
    count = 0
    for n in nums:
        if n % 2 == 0:
            ret.append(n)
        count += 1
    return ret

Hope it works for you :slight_smile:


#6

Ugh, can't believe it was something silly like that. Thanks, that fixed the issue.


#7

Do you mind explaining why this works with or without the count = 0 variable?


#8

since I just did the count variable for debugging reasons it works without it also. As you can see it just counts how many times the for loop gets executed.


#9

Try this! It worked for me:

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


#10
def purify(num):
    ans = []
    for x in num:
        if(x%2 == 0):
            ans.append(x)
    return ans

print purify([1,2,3,4,5,6,7,8,9])