Purify: Thats crazy and i cant see why!


#1

Guys please look at this and see how it places an even numbers list in my list!! Where does this full of even numberslist come from!?? Just to debug where on earth those even numbers get in my list i appended my list "e" with the input list, "numbers", and it still places an even numbers list first!!

odd=[1, 3, 5, 7, 9]
e=[]
def purify(numbers):
  #  for i in numbers:
  #      if i not in odd:
            e.append(numbers)
            return e

The interpreter message is this: Oops, try again. Your function fails on purify([1]).
It returns [[2, 4, 6, 8], [1]] when it should return []


#2

You are appending the input list, rather than the single number of each iteration.

even = []
for i in numbers:
    if i not in odd:
        even.append(i)

Be sure to set even inside the loop so it is starts empty each time the function is called. That is why the even numbers are still there.


#3

Uff thank you that really helped a lot.. at beginning i have coded just the same as you did, later to debug i changed it.. but this last warning solved the problem: to get the list definition INside the function!

Thank you:)


#4

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