12. Purify


I don't know why my code couldn't work.
Could anyone explain the reason to me?

Oops, try again. Your code looks a bit off--it threw a "list assignment index out of range" error. Check the Hint if you need help!

def purify(numList) :
    evenList = []
    n = 0
    for x in numList :
        if x % 2 == 0 :
            evenList[n] = x
            n += 1
    return evenList

print purify([1,2,3,4,5])


to do x % 2 == 0 x needs to be integer so insert x = int(x) before
and then just append to your list with evenList.append(x) and thats it
you dont need n variable there. bosh


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


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