Purify even numbers from list of numbers


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/purify?action=lesson_resume

<In what way does your code behave incorrectly? Include ALL error messages.>

throws up the error - Your code looks a bit off–it threw a “list index out of range” error. Check the Hint if you need help!

<What do you expect to happen instead?>

purify even numbers from list of numbers

def purify(numset) :
  newset = []
  for i in range(len(numset)) :
    if i % 2 == 0 :
      return newset[i]
    else :
      return numset
    print purify([1,2,3])

#2

there are several problems with your code

the function call:

print purify([1,2,3])

executes the function, so it should be placed outside/after the function body/content. Having said that, adding the function call is good practice so you can actually see what the function is doing :slight_smile:

by default, a function returns none at the end of the function, if we want to return something else at the end of function, we can use the return keyword. So, the function ends when a return keyword is reached, so your function ends in the first iteration of the loop, what if the list contain 10 elements?

using range() will give indexes for the list items, not values from the list. Why would you want indexes to solve this problem?


#3

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