+ 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)
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
+ 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
+ 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)
+ 2
I forget to reach out. THX guys, Now Its simple for me. đ
+ 1
What do you get as the output??
+ 1
while n < len(items):
if items[n] % 2 == 0:
sum += items[n]
n += 1