+ 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)
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
+ 8
How about
print(sum(n for n in items if n % 2 == 0))
🙂
+ 1
See short solution from David Ashton or change your code in loop:
if items[n]%2 == 0:
sum += items[n]
n += 1
0
list=[23,555,666,123,128,4242,990]
sum=0
for i in list:
if i%2==0:
sum+=i
print(sum)