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.
4/8/2021 1:26:35 AManthony silver
12 AnswersNew Answer
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.
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.
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
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.
You can check for even and odd numbers using % operator. if a%2==0, even else odd
Yes you are right , but the problem is that I don't know how to make it work with the program
ChaoticDawg this is the task
Ok , can i show you a code and you tell me if it's right?
Sorry guys , my internet fall down yesterday
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)
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