12.purify


#1



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


Hello everyone, I am stuck on exercise 12. purify. The error message displayed reads:

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


I'm not very good at coding and I often have to use the help forums. Any advice on how to change my code to make it work would be much appreciated.


Replace this line with your code. 
def purify(listnum):
   for num in listnum:
        if num % 2 != 0:
            
            listnum.remove(num)
            return listnum


#2

Edit: I changed the code a bit:

def purify(listnum):
    for num in listnum:
        new_list = []
        if num % 2 == 0:
            
            listnum.remove(num)
            new_list.append(num)
        return new_list
        ```

#3

New Error Message:

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


#4

Problem1.
define new_list = [] before your for loop,every time when loop goes over,it sets it to empty list again.

Problem2.
listnum.remove(num)

back to instruction..
Do not directly modify the list you are given as input;
so no need of this line as you already have a new list.

problem3.
Indention of return statement is need to be fixed.


#5

Thank you so much, it works! Thank you for explaining why it was flawed as well :slight_smile:


#6

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