+ 1

I want to sum only even numbers in the list...

can anyone please help me to do this.. i tried it ...but not working... items = [23, 555, 666, 123, 128, 4242, 990] sum = 0 n = 0 while n < len(items): num = items[n] n +=1 sum += num if num%2==0: continue print(sum)

18th Oct 2020, 3:21 AM
Rahul Prasad
Rahul Prasad - avatar
4 Antworten
+ 2
Your code's adds all the number in the list.If you want to do sum of the even number your code should be like that: https://code.sololearn.com/cpUv6H7z3PDc
18th Oct 2020, 4:53 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 8
How about print(sum(n for n in items if n % 2 == 0)) 🙂
18th Oct 2020, 3:26 AM
David Ashton
David Ashton - avatar
+ 1
See short solution from David Ashton or change your code in loop: if items[n]%2 == 0: sum += items[n] n += 1
18th Oct 2020, 4:51 AM
Nikolai Ivanov
Nikolai Ivanov - avatar
0
list=[23,555,666,123,128,4242,990] sum=0 for i in list: if i%2==0: sum+=i print(sum)
19th Oct 2020, 5:28 PM
Rowdy Reddy
Rowdy Reddy - avatar