+ 1

Python Core 26.5 "Free Stuff!"

I'm not sure what to do here: A shop is running a promotion today! If an item's price is an odd number, you get that item for free! You use a list to store the prices of all items in the shopping cart. The given code uses a while loop to iterate over the list, calculates the price of all the items in the list, and output the result. Change the code to skip the odd prices, calculate the sum of only the even prices and outputs the result. Use the continue statement to skip the loop iterations, where the number is odd. 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)

19th Jul 2021, 5:13 PM
Cory Peitsch
Cory Peitsch - avatar
2 Respostas
+ 1
Skip the iteration for those prices that are odd by adding the following statement at the very top inside while loop, If items[n] % 2 != 0 : continue
19th Jul 2021, 8:19 PM
Abhay
Abhay - avatar
0
#or do it like this if num % 2 == 0: sum += num
20th Jul 2021, 3:17 AM
Simba
Simba - avatar