-3

I have this assignment:

Define a function that takes in an arbitrary number of arguments, and returns a list containing only those arguments that are even. Don't run the function simply provide the definition.

What I've tried:

def myfunc(*args):
    return list(args%2==0)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Meah
  • 1
  • 2

4 Answers4

0

It should be as follows:

def a (*args):
  l = []
  for i in args:
    if i%2 == 0:
      l.append(i)
  return l
Puja Neve
  • 50
  • 7
0
 def myfunc(*args):
     a=[]
     for num in args:
         if (num%2 == 0):
             a.append(num)
     return a `
matthias_h
  • 11,356
  • 9
  • 22
  • 40
Ajinkya
  • 1
  • 1
  • While this may answer the question, it was flagged for review. Answers with no explanation are often considered low-quality. Please provide some commentary in the answer for why this is the correct answer. – Dan Mar 26 '20 at 03:37
-1

I think this should work.

def a (*args):
  l = []
  for i in args:
    if i%2 == 0:
      l += [i]
  return l
ib4real
  • 84
  • 5
-1

Not sure if it is requires to be as arguments like in range(), iba4read's post shuld work just fine. Just in case i'll give you an examplie with a list, note that a ´tuple´ not alwais works as a list.

import random

array = []
for r in range(random.randint(2, 10)):
    array.append(r)


def even_numbers_of(array):
    new_array = []
    for i in array:
        if i % 2 == 0:
            new_array.append(i)
    return new_array


print(even_numbers_of(array))

SrPanda
  • 854
  • 1
  • 5
  • 9