+ 2

I'm having problems with the " free thing's" task

A store is doing a promotion today! If the price of an item is an odd number, you will get that item for free! A list is used to store the prices of all the items in the shopping cart. The given code uses a time loop to iterate over the list, calculates the price of all the items in the list, and produces the result. Change the code to skip the odd prices, calculate the sum of only the even prices and get the result. Use the continue statement to skip the loop iterations, where the number is odd. they give me this code as an example: 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) The problem is that i don't understand how it works, the last time when you give me the code of the other task i understand how it work , but this is the first time I see this time of code, if you help me it would be awesome.

8th Apr 2021, 1:26 AM
Alan Restrepo
+ 3
The given code finds the sum of all prices but they are asking to skip the odd prices So add : if(num%2==0): Above: sum+=num
8th Apr 2021, 9:36 AM
Arun Ruban SJ
+ 3
ChaoticDawg Hi bro, i hope you are doing well, you were right, i did it that way and it work, thanks for explaining me how the program worked, as I tell you it was the first time that I saw a program like that, but I understand it after you explain it, thanks again, it's good to have someone that helps, Jesus Christ blessed you bro.
8th Apr 2021, 5:44 PM
Alan Restrepo
+ 2
Use the same amount of indentation for each level. You are only using 1 space for continue in your if statement, where you are using the more standard 4 spaces (tab) elsewhere. Just stick to using 4 spaces for each level of your indentation and you should be good.
8th Apr 2021, 5:36 PM
ChaoticDawg
+ 1
anthony silver As Hima stated you use the modulo operator in an if statement. If the number is even add its value to the total. I don't recommend using 'sum' as a variable name as it will overwrite the built-in function sum(). The while loop is saying to keep looping as long as the current value of 'n' is less than the length len() of the list 'items'. The line: num = items[n] Uses n as the index to get the value from the list at that index. Then the value of n is incremented by 1. Here is where you need to add the if statement so that 'num' is only add to the total (sum) if it is even. Then after the loop finishes print the final value.
8th Apr 2021, 3:55 AM
ChaoticDawg
0
You can check for even and odd numbers using % operator. if a%2==0, even else odd
8th Apr 2021, 3:13 AM
Hima
0
Yes you are right , but the problem is that I don't know how to make it work with the program
8th Apr 2021, 3:18 AM
Alan Restrepo
0
ChaoticDawg this is the task
8th Apr 2021, 3:28 AM
Alan Restrepo
0
Ok , can i show you a code and you tell me if it's right?
8th Apr 2021, 4:10 AM
Alan Restrepo
0
Sure
8th Apr 2021, 4:26 AM
ChaoticDawg
0
Sorry guys , my internet fall down yesterday
8th Apr 2021, 5:22 PM
Alan Restrepo
0
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 sum += num print(sum)
8th Apr 2021, 5:22 PM
Alan Restrepo
0
This code work , and give me the right result for the task but in the code play ground, but when I'm gonna use it in the in the task it tell's me "unident does not match anyouter identation level" you guys know why
8th Apr 2021, 5:29 PM
Alan Restrepo
- 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): continue sum += num print(sum)
6th Feb 2022, 10:21 AM
Jake Ambrose