My code throws a "'NoneType' object is not iterable" error


#1

According to the feedback window, my code throws a "'NonType' object is not iterable" error. The task was to define a function that only returns the even numbers in the input. So I created an empty list, "new_num" to put the results in, and then looped through the input, "num", to append the even numbers into "new_num".

Although I just started learning python about a month ago, I don't really understand the reason why my code doesn't work. Thanks for pointing out my errors.

def purify(num):
    new_num=[]
    for i in num:
        if i%2==0:
            new_num.append(x)

#2

@scriptsolver75014 You use the variable "i" in your for loop:

but not when you are appending it to the new list:

Change the "x" variable to "i" and you should be fine. :slight_smile:


#3

Oh sorry, that was me trying to fix the problem by changing the "x" to "i" (apparently didn't corrected all of them though xD).
It still sends an error message even if I followed your instructions, stating that: "Oops, try again. Your function crashed on [1] as input because your function throws a "'NoneType' object is not iterable" error."


#4

You have no return statement. I forgot to mention that in the last post I wrote. :sweat_smile: It should say:

return new_num

#5

Thanks a lot, solved my problem!
The error message was quite mis-leading though :joy:


#6

You're welcome!:laughing: Keep coding!

-- @chipjumper36731


#7

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