12/15 can someone tell me what is wrong with this

def purify(item):
    length = len(item)
    if length == 1:
        if item[0] % 2 != 0:
            del item[0]
            return item
        for i in range(length - 1):
            if item[i] % 2 != 0:
                del item[i]
        return item

when i run this, [4,5,4] is returned instead of [4,4] when the input was [4,5,5,4]. i cannot understand what is going wrong


From what I see you are tying to see if a given number is even from a list or a single entry.

Here is some code for you to study. If you have any questions on any of it's functions ask away.


def is_even(numbers):
        return [number for number in numbers if number % 2 ==0]
    except TypeError as error:
        print("You entered only a single entry")
        if numbers % 2 == 0:
            return numbers
            print("Your entry was not even")


I should mention that the try statement does what it says on the tin, if it fails it will throw an exception and we can using the except statement then to do something useful other than crashing. You can also use else and finally with the try statement.



If you read the code will tell you that you have one entry and return the value if it is even. I put an example of what happens when the code is called you can try it your self to see it in action.


a = [1, 1.22, 3, 4, 7, 8, 20, 2123.451]

b = 7

c = 4


#For A
[4, 8, 20]

#For B
'You entered only a single entry'
'Your entry was not even'

#For C
'You entered only a single entry'


what if the list contains only one number/item? according to your code, will it print the error message notifying that the list contains only one number?
and can nothing else be done to handle that situation?


To keep things simple, try understanding the following code:

def purify(numbers):
    newlist = []
    for i in numbers:
        if i % 2 == 0:
    return newlist