Purify .. Your function fails on purify([1]). It returns [1] when it should return []


#1

I do understand that its getting 0.5 but really do not know a way around it. Help HELP! Lol

def purify(list):
    new_list = []
    
    for number in list:
    
        if number%2 != 0:
            new_list.append(number)
    return new_list
    
    

print purify([1,5,9,17,23,38,4,2])

#2

I would be happy to help you, but I do not have access to the exercise. May I ask you to explain what are the requirements for this function? What this function should return?


#3

Thanks! Here 's the exercise:

Define a function called purify that takes in a list of numbers, removes all odd numbers in the list, and returns the result.

For example, purify([1,2,3]) should return [2].

Do not directly modify the list you are given as input; instead, return a new list with only the even numbers.


#4

This condition:

number%2 != 0

is True for odd numbers, so you just need to... reverse it:

number % 2 == 0

#5

Well if I understand this correctly the exercise is trying to get only ODD numbers ... that´s why I used the !=0 rather the == 0 (this way I would get Even numbers)


#6

Define a function called purify that takes in a list of numbers, removes all odd numbers in the list, and returns the result.

So you need to return the list of even numbers.

In the if block you are adding correct values to the resulting list. So your condition must accept correct values (even numbers) and filter out the odd numbers. At this moment you are doing a reverse operation.


#7

Lol I ve just realised my mistake! (SOOO DUMB) Thanks! In fact how I wrote it was correct. I only needed to change the != to == and that s it! :slight_smile:


#8

This is exactly what I was trying to say. You're welcome :slight_smile:


#9

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