0
FreeStuff! python core 26.5 practice.. help
So for this exercise we get: items = [23, 555, 666, 123, 128, 4242, 990] sum = 0 n = 0 while n < len(items): num = items[n] n += 1 sum += num print (sum) we are then asked to change the code in the appropriate way to ignore any odd numbers and output the sum off the even numbers. In the help at the bottom of the exercise we are asked to use the continue statement to skip loop iterations. any suggestions?
5 odpowiedzi
+ 1
items = [23,555,666,123,128,4242,990]
sum = 0
n = 0
while n < len(items):
num = items [n]
n += 1
if num %2 == 0:
sum += num
else:
continue
print (sum)
This is how you use it in a while loop where is the other guy shows you a for Loop however if you play with this for a second you'll also notice that if you release the else statement and run the code without it it runs the same this particular example as far as I am able to figure out doesn't actually need the continue statement in order to work correctly
+ 1
items = [23,555,666,123,128,4242,990]
sum = 0
n = 0
while n < len(items):
num = items [n]
n += 1
if num %2 == 1:
continue
else:
sum += num
print (sum)
This is how I believe that the assignment is to be completed instead of saying what's divisible by 2 with a remainder of 0 what I want to do is say if the number is odd skip go back can only add the ones that are even
0
You can use condition to check type of number
items = [23, 555, 666, 123, 128, 4242, 990]
sum = 0
for num in items:
If num % 2==0:
sum += num
else:
continue
print (sum)
0
items = [23, 555, 666, 123, 128, 4242, 990]
sum=0
for i in range(0,len(items)):
if(items[i]%2==0):
sum=sum+items[i]
else:
continue
print('sum of even numbers: ',sum)
0
# https://www.sololearn.com/learning/1073/2281/4991/1
# Problem i had trouble with, tough odd item free code.
items = [23, 555, 666, 123, 128, 4242, 990]
sum = 0
n = 0
while n < len(items):
num = items[n]
n += 1
if(num % 2 != 0):
continue
sum += num
print(sum)