0
Help please python core lesson 26.5
Use a list to save prices for all items in your cart. There is code that uses a while loop to iterate through the list, calculates all the items in the list, and prints the result. Change the code to skip odd prices, calculate the sum of only even prices, and display the result. 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: sum += num continue print(sum)
5 Answers
+ 3
You rather need the modulo operator % instead if floor division// to check if the remainder == 0
+ 2
If num//2 == 0 will never be true, because no number divided by 2 is 0. Try the % operator instead :)
+ 2
Ivan
You have to skip those numbers which are odd number so do this
if num % 2 == 1:
continue
sum += num
And also there is Indentation problem. print statement should be outside the loop
+ 2
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
continue
print(sum)
Thanks đ
In print(sum) was error
+ 1
Delicate Cat, already decided đ. For haven't learned yet, next lesson