12.Purify


#1

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

Ok so here is my code:

def purify(*args):
    args=[1,2,3]
    for i in args:
        if i % 2 == 0 and i != 1:
            return [i]
        print args 
print purify()

And this is what is displayed to console:
[1, 2, 3]
[2]
None

And here is the errno message that I'm recieving
Oops, try again. Your function fails on purify([1]). It returns [2] when it should return [].

In the exercise example it states - "For example, purify([1,2,3]) should return [2]"

My code is returning the only even int so.... why is this 'wrong'?


#2

The exercise runs a number of lists to see your function is good. Currently, it only works for a specific list

Try to write one which works for all lists. If you need more help after you tried this, pots an updated version of your code in a new reply


#3

Thank you for the reply.

Also, I realize that my code was not displaying even numbers greater than two as the list expanded. The problem was the missing print statement for i.

def purify(*args):
    args=[1,2,3,4,5,6,7,8,9,10]
    for i in args:
        if i % 2 == 0 and i != 1:
            print [i],
print purify()

#4

This still won't work, for several reasons: What if there are odd number of items in the list? The exercise wants you to return a list of all the even number. You don't create a list with the results. Why not simple pass the list on the function call:

def purify(args):
    for i in args:
        if i % 2 == 0 and i != 1:
            print [i],
print purify([1])

#5

I still got this errno message-

Oops, try again. Did you create a function called product? Your code threw a "global name 'product' is not defined" error.

Although I'm pretty sure that I do understand the logic here; It almost seems as though lists were never meant to pass directly through pythons function parameters. It would seem that they are better served as object variables.

The code that i posted earlier in fact does only grab numbers divisible by two-and writes them to a new list; unfortunately that wasn't the goal of the exercise i guess.


#6

Please post an updated version of your code


#8

Thank you for this post! The re-factored script is light and efficient. Perfect!