Purify function


#1




Your function crashed on [1] as input because your function throws a "'int' object is not iterable" error.


I expect to count the total number of even numbers in a list of numbers.


def purify(numbers):
    total = 0
    if len(numbers) <= 1:
        if numbers[0] % 2 == 0:
            total = 1
        else:
            total = 0
    else:        
        for i in numbers:
            if i % 2 == 0:
                total += 1
    return total


#2

I added lines 3-8 to try to fix the problem but the function doesn't like lists of length 1.


#3

Your input is a list of numbers, I suggest just making a loop to iterate through the list (numbers). As in keep everything under your recent else statement.


#4

Now, what I see is that if the number is even, you increment a count. But that's not what the lesson is asking. It just wants a list of even numbers only in the end.

purify([1, 2, 3, 4, 5]) => [2, 4]


#5

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