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)

27th Aug 2021, 7:45 PM
Ivan
5 Respostas
+ 3
You rather need the modulo operator % instead if floor division// to check if the remainder == 0
27th Aug 2021, 7:47 PM
Lisa
Lisa - avatar
+ 2
If num//2 == 0 will never be true, because no number divided by 2 is 0. Try the % operator instead :)
27th Aug 2021, 7:47 PM
Chloe
Chloe - avatar
+ 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
27th Aug 2021, 7:51 PM
A͢J
A͢J - avatar
+ 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
27th Aug 2021, 7:56 PM
Ivan
+ 1
Delicate Cat, already decided 😄. For haven't learned yet, next lesson
28th Aug 2021, 5:11 AM
Ivan