List of even numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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)

5th Feb 2022, 3:39 AM
Knowledge Is Power
Knowledge Is Power - avatar
3 Answers
+ 1
list = [1,3,6,8,2,4] e = [] for i in list: if i%2==0: e.append(i) print(e)
5th Feb 2022, 3:59 AM
JOKER
JOKER - avatar
+ 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))
5th Feb 2022, 4:01 AM
L.M.Paredes
L.M.Paredes - avatar
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👍
6th Feb 2022, 3:11 AM
Knowledge Is Power
Knowledge Is Power - avatar