Purify: I can't return from my else statement


#1




I'm trying to get my code to return when my if statement isn't met. In other words, when my loop gets to the else statement, it throws the following error:

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


I expected the code to return from the else statement and back into the for loop.
I can replace my else: return with an else: print('') and it runs fine. I don't understand why that is.


def purify(dirty):
    clean=[]
    for i in range(len(dirty)):
        if dirty[i] % 2 == 0:
            clean.insert(i, dirty[i])
        else:
            return
    return clean


#2

why would you do this? a return keyword will cause the function to end (even while in a loop)


#3

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