12. purify call function


#1

Can someone please tell me ho do i call this function?
The code is correct but i dont know how to call it. Thanks!

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


#2

You can use the following statement:
print(purify([1,2,3]))
It worked for me.


#3

I think you need "else: continue"

if n % 2 == 0:
new.append(n)
else:
continue


#4

I don't think else statement is needed for this as it only requires you to send one list

I came up with this:

def purify(uInput):
    even = []
    for i in uInput:
        if (i % 2 == 0):
            even.append(i)
    return even
    
print purify([1,2,3])

And function can be called the way I've called print purify([1,2,3]) with user input added into the function parameter as a list.


#5

thanks Mohammed..nice code here :grin:


#6

Here's how I did it.

def purify(sequence):
sequence = list(sequence)
result = []
for i in sequence:
if i % 2 == 0:
result.append(i)
else:
continue
return result


#7

Hey Mohammed7690,

First, thank you for saving my sanity.

I was scrubbing your code against my old iterations... and found that I had the same idea... a couple times.. :expressionless:

But, here's the kicker I was trying to store my output in another variable because I didn't know you could just append without storing the data... well, in a for loop anyway...

Two questions:

  1. Do you need the parenthesis in your if statement?
  2. Why does this not work:
    def purify(list_of_numbers)
    even = []
    for i in list_of_numbers:
    if i % 2 == 0:
    even = even.append(i)
    return even

Thanks a bunch,
Gr33n

PS the indents are there when I edit?


#8

def purify(numbers):
return filter(lambda x : x % 2 == 0, numbers)