Purify Code runs and works but I get an error from codeacademy


#1



I am stuck on the Practice Makes Perfect code which is supposed to return a list of even numbers, The exercise is called Purify

The Error I get is
Oops, try again. Your function fails on purify([1]). It returns [2, 4, 2, 4, 6, 8] when it should return [].

It does return the right numbers so I am not sure what is wrong with it.


new_numlist = []
num_list = [1,2,3,4]


def purify(num_list):
    count = 0
    list_len = len(num_list)
    for numbs in range(0,list_len):
        even = num_list[numbs]%2
        if even == 0:
            new_numlist.append(num_list[numbs])
    num_list = new_numlist
    return num_list

            

result = purify(num_list)
print result


#2

This list is not being reset at the start of each call to the function. Declare it inside the function as a local variable.


#3

below is my code : i am getting following error
Oops, try again. Your function crashed on [1] as input because your function throws a "'NoneType' object is not iterable" error.


def purify(getlist): #read list
evenlist = [] #create empty even no list
oddlist = [] #create empty odd no list
for i in getlist: # iterate through list characters
if ( i % 2 == 0): #check for even or add
evenlist.append(i) #if even append even list
else:
oddlist.append(i) #if odd append odd list
print (evenlist) #print even list
print (oddlist)#print odd list
purify([1,2,3,4,5,6,7,8,9,10]) #call function


#4

Don't think you need oddlist at all. It just over-complicates the function. You are also missing return statement(s) (make sure you are indenting the return statement properly as in lining it up with the for loop statement).

Also, you don't need to add brackets around your conditional statements. It won't affect anything major but it's not necessary.


#5

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