Possible Bug in 'Purify-Practice Makes Perfect!'


#1


This is the link to the exercise where I am stuck at.

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


Below is the following code. I do not receive any errors while executing this code in ipython, or ipython3.


num = [1,1,1,2,3,4,5,6,7]

def purify(num_list):
    new_list = num_list
    for i in new_list:
        if i%2:
            new_list.remove(i)
            purify(new_list)

If I define the function, and then execute the following code:

purify(num)

print(num)

the output is given as 

[2,4,6]  which is exactly what I want.


The error I receive from codecademy is:
 "Oops, try again. Your function crashed on [1] as input because your function throws a "'NoneType' object is not iterable" error. "

But executing  purify([1]) gives me no error in python 2.7 or python 3.4

Is this an actual bug or am I forgetting something?


#2

It is worth noting that new_list is not a copy of num_list but a reference to it. To create a discrete copy, use slicing (num_list[:]) or the .copy() method (num_list.copy()).


#3

There is a good chance it wants you to return a value with your function.

Example:

num = [1, 2, 3, 4, 5]

def purify(num_list):
    nList = []
    for i in new_list:
        if i % 2 == 0:
            nList.append(i)
    return nList

Generally when you create a function you will want to return a thing because if you don't there is no reason to write a function.

EDIT:

Also a function that does not return anything explicitly returns a NoneType object and that is where your error is coming from.


#4

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