Why my code dont work?


#1

why my code dont work?
it need to take a list and return a list with only even numbers



def purify(numbers):
  for i in numbers:
    if i % 2 != 0:
      numbers.remove(i)
  return numbers



#2

Do not directly modify the list, as mentioned in the instructions. Removing items from the list, will modify the list

furthermore, removing items from the list is a difficult approach given you deal with index shift which would cause two successive odd numbers to be skipped, as we can see:

def purify(numbers):
  for i in numbers:
    if i % 2 != 0:
      numbers.remove(i)
  return numbers

print purify([4,5,5,4]) # prints [4,5,4], which is not right

#3

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