My instructions in a codecademy project are to
Define a function called purify that takes in a list of numbers, removes all odd numbers in the list, and returns the result. For example, purify([1,2,3]) should return [2]. Do not directly modify the list you are given as input; instead, return a new list with only the even numbers.
The code I have come up with is:
def purify(numbers):
pure = numbers
for num in pure:
if num % 2 != 0:
pure = pure.remove(num)
return pure
And the error is:
Oops, try again. Your function crashed on [1] as input because your function throws a "'NoneType' object is not iterable" error.
As I've come to understand it, this means something in my code is being returned as "None". Or there is no data there. I can't seem to find what's wrong with this short and simple code, unless it's in my if statement. Thanks in advance.
So I made the edit within the code to remove "pure = pure.remove(num)" and that solved the none issue. However, when running the code it still fails the input of [4,5,5,4] and returns [4,5,4].
New Code:
def purify(numbers):
pure = numbers
for num in pure:
if num % 2 != 0:
pure.remove(num)
return pure