Problems with purify #just_priest_things


#1


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

"'int' object is not iterable"


Hello,
I fon't know why my code isn't working, any help is appriciated.

Task:
Define a function called purify that takes in a list of numbers, removes all odd numbers in the list, and returns the result.


def is_even(x):
    x = int(x)
    if x %2 == 0:
        return True
    else:
        return False
        

def purify(y):
    n = []
    for i in y:
        i = int(i)
        if is_even(i) == True:
             n += i


#2

They way you did it seems strange to me. Either way, I think your problem is coming from where you n += i. You're trying to add an int to a list. Try using n.append(i). Also, don't forget to return your evens list! return n


#3

I changed it to:

def is_even(x):
x = int(x)
if x %2 == 0:
return True
return x
else:
return False

def purify(y):
n = []
for i in y:
i = int(i)
if is_even(i) == True:
n.append(i)

but now I'm getting:

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


#4

Another thing is that your is_even function has two returns in the if statement. That's not really going to cause an error, but the second return (return x) will never run because the statement ends when it returns. Since it's very easy to check if a number is even, I would just include the if x % 2 == 0 in the purify function. So for each number in the list, if that number is even, append each one to a new list, and if it's not, just don't do anything.

def purify(y):
  n = []
  for i in y:
    if i % 2 == 0:
      n.append(i)

#5

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