15. practice makes perfect


def purify(even):
numbers = []
for number in even:
if not (number in even):
numbers += number
return numbers


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

def purify(even):
    numbers = []
    for number in even:
        if not (number in even):
            numbers += number
    return numbers


incorrect, pls help correct it


First of all in python formatting matters so please, indent your code and to make it better readable wrap it in such an environment:

your code

next up add a link to the exercise or at least explain what your input is. According to this line:

for number in even:

it is not a single number.

Also if numbers is a list then numbers += number might not work. So please give some more information.


I have corrected it. Done @haxor789


Ok the first thing is that you should think of a better idea on how to find even numbers, because this doesn't work:

    for number in even:
        if not (number in even):

Because what it means is:
Do this for every element of even: Take a number from the list named even and check if this number is not in the list even. It obviously is in the even list because you took it from that list the very line before :slight_smile:
Therefore you'll always end up with an empty list.

And the other thing is that when you want to add a new element to a list you're better off using LISTNAME.append(number) instead of numbers+=number


like this:

     def purify(even):
        new = []
        for i in even:
            if i%2==0:
        return new


Yes, that should be much better.


thanks @haxor789 .:wink:


can you also help with this:

def product(integers):
for i in integers:
if i*i:
return integers
print integers

error message:
Oops, try again. Your function does not return an int.

that is for the next lesson on product, i am trying to multiply the numbers in : if i*i, but i know its not correct.


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