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

#1
def purify(item):
length = len(item)
if length == 1:
if item[0] % 2 != 0:
del item[0]
return item
else:
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

#2

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.

MY CODE

def is_even(numbers):
try:
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
else:
print("Your entry was not even")

EDIT:

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.

EDIT 2

@dataplayer09385

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.

INPUTS

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

b = 7
is_even(b)

c = 4
is_even(c)

Output

#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'
4

#3

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?

#4

To keep things simple, try understanding the following code:

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