For loop not going over each element in a list


#1



Hello, I'm having a bit of trouble with this for loop. I'm fairly sure its just a simple mistake I'm glossing over, but I'd appriciate some help in any case.

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

The error message I'm receiving is:

Oops, try again. Your function fails on purify([4, 5, 5, 4]). It returns [4] when it should return [4, 4].


The code I'm running doesn't seem to be "seeing" each element on the list. It finds the first number that is even, appends it to the list, and then stops.

def purify(numbers):
    new = []
    for c in numbers:
        if c % 2 == 0 and c != 0:
            new.append(c)
        return new
    else:
        new.append()


#2

as soon as a return keyword is reached, the function ends. In your case, in the first run of the for loop, a return keyword is reached, which means the loop and function ends.

You might want to reconsider where to put your return, if you need further help, let me know (post a reply with your updated code)


#3

Thanks so much! I Can't believe it was as easy as moving the "return". Code works fine now.


#4

no problem, you could also just remove else altogether. It isn't doing anythign