+ 2

Python challenge

I need to sum only even numbers from list. why my code doesnt work? Pls be kind im trying. :D items = [23, 555, 666, 123, 128, 4242, 990] sum = 0 n = 0 while n < len(items): if items[n] % 2 == 0: num = items[n] n += 1 sum += num else: continue print(sum)

5th Dec 2021, 7:35 AM
KryĆĄtof VĂĄvra
KryĆĄtof VĂĄvra - avatar
6 Answers
+ 1
Thank you for your brilliant answer, you've helped me a lot. But one question more, please: Where and when could we have worked with "Continous" as the problem sais: "Use the continue statement to skip the loop iterations, where the number is odd". Thank you for your help
30th Dec 2021, 4:40 PM
josé manuel
josé manuel - avatar
+ 4
KryĆĄtof VĂĄvra You are incrementing n inside if condition which would be never increase if your condition will not satisfy. So result would be No Output because of infinite times runs. Also doesn't make sense to use continue in else part. Also don't use python function as an identifier So do this: while n < len(items): if items[n] % 2 == 0: sum1 += items[n] n += 1
5th Dec 2021, 8:42 AM
AÍąJ
AÍąJ - avatar
+ 2
#try this items = [23, 555, 666, 123, 128, 4242, 990] sum = 0 n = 0 while n < len(items): if items[n] % 2 == 0: num = items[n] sum += num n += 1 print(sum)
5th Dec 2021, 7:54 AM
Simba
Simba - avatar
+ 2
I forget to reach out. THX guys, Now Its simple for me. 😇
23rd Dec 2021, 1:08 PM
KryĆĄtof VĂĄvra
KryĆĄtof VĂĄvra - avatar
+ 1
What do you get as the output??
5th Dec 2021, 8:00 AM
Reuben Nyirenda
Reuben Nyirenda - avatar
+ 1
while n < len(items): if items[n] % 2 == 0: sum += items[n] n += 1
5th Dec 2021, 8:39 AM
SoloProg
SoloProg - avatar