+ 1

How to solve the error??

a = int(input()) x = 0 y = "" for i in range(a): b = input() y = y+b for j in y: if j%2==0: x = x+j print(x) Here is the question: You want to take a list of numbers and find the sum of all of the even numbers in the list. Ignore any odd numbers. Task: Find the sum of all even integers in a list of numbers. Input Format: The first input denotes the length of the list (N). The next N lines contain the list elements as integers. Output Format: An integer that represents the sum of only the even numbers in the list. Sample Input: 9 1 2 3 4 5 6 7 8 9 Sample Output: 20

10th Jul 2021, 4:57 PM
Shahir
Shahir - avatar
3 Answers
+ 4
Shaik.Shahir it's simple a = int(input()) x = 0 for i in range(1, a + 1): b = int(input()) if (b % 2 == 0): x = x + b print (x)
10th Jul 2021, 5:02 PM
AÍąJ
AÍąJ - avatar
+ 4
If you just want to add all the even numbers in a list, e.g. my_list, you could do print(sum(n for n in my_list if not n % 2)) 🙂 not n % 2 is the same as n % 2 == 0
10th Jul 2021, 5:53 PM
David Ashton
David Ashton - avatar
0
Shaik.Shahir Here's a possibility: print(sum(filter(lambda x: not x & 1, [int(input()) for i in range(int(input()))]))) # Hope this helps
10th Jul 2021, 6:38 PM
Calvin Thomas
Calvin Thomas - avatar