0
List of even numbers
#Write a function which accepts an input list of numbers and returns a list which includes only the even numbers in the input list. If there are no even numbers in the input numbers then your function should return an empty list. sample_list = [1,3,6,8,2,4] def even_num(x,y): e=[] for i in x: if i % 2 ==0: y.append(i) return y else: return e even_num(sample_list,e)
3 odpowiedzi
+ 1
list = [1,3,6,8,2,4]
e = []
for i in list:
if i%2==0:
e.append(i)
print(e)
+ 1
sample_list = [1,3,6,8,2,4]
def even_num(x,y): # why y? the function only needs the list to treat
e=[]
for i in x:
if i % 2 ==0:
y.append(i) # if you declare the variable "e" why store in y
return and # if return is placed this kills the process and returns the value
else:
return e # returns empty "e" [ ]
even_num(sample_list,e)# e is being declared inside the function, so it is not accessible from outside the function
sample_list = [1,3,6,8,2,4]
def even_num(x,y):
e=[]
for i in x:
if i % 2 ==0:
y.append(i)
return y
else:
return e
even_num(sample_list,e)
corrected
sample_list = [1,3,6,8,2,4]
def even_num(x):
e=[]
for i in x:
if i % 2 ==0:
e.append(i)
return e
print(even_num(sample_list))
0
Thank you so much!! Its working. I found my some of syntax error too thats why it wasn’t working. Your answer was very helpful👍