 32.2 Let’s go shopping practice help | Sololearn: Learn to code for FREE!

0

# 32.2 Let’s go shopping practice help

So lost. I’m not finding that the lessons prior are aligned to the practice, and with little direction from the app too. cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 for cart in cart: total = cart - (cart * discount/100) print(total) You’re making a shopping cart program. The shopping cart is declared as a list of prices, and you need to add functionality to apply a discount and output the total price. Take the discount percentage as input, calculate and output the total price for the shopping cart. Use a for loop to iterate over the list. Use the following formula to calculate the result of X% discount on \$Y price: Y - (Y*X/100)

+7

You can't say for cart in cart: It has to be for item in cart: Also, you are printing the total for each item. What you need to do is sum each of the totals and print the final sum at the end. Just keep persevering. It will all become clearer the more you practice, I promise.

+5

From the sentence "output the total price for the shopping cart", I think you need to output the sum of the cart which each item is discounted. Try += instead of = in the for loop, and get the print(total) out of the for loop to output the total price only once.

+3

item gets defined separately for each iteration of the loop. cart = [15, 42, 120, 9, 5, 380] for item in cart: On the first iteration, item takes the value 15, on the second iteration, item becomes 42, and so on. So you don't need to define item beforehand. It gets defined within the loop.

+2

This took me a while to solve as each line would print a total not just a final one. Here's what worked for me. cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 for item in cart: total+=item-(item*(discount/100)) else: print(total)

+1

c=[15,42,120,9,5,380] d=int(input()) t=0 for i in c: t+=i*(1-d/100) print(t) Hope this helps!

0

thanks CarrieForle. the combo of those worked. here is the code. but how does it work without item being defined? Russ cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 for item in cart: total += item - (item * discount/100) print(total)

0

I got a big problem. Why do I get every iteration in total rather than the total of the whole set of item? cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 for item in cart: total += item - (item * discount/100) print(total) Output: Enter a number: 12 13.2 50.16 155.76 163.67999999999998 168.07999999999998 502.47999999999996

0

cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = sum(cart) for item in cart: total -= item * discount/100 print(total)

0

For those of you getting more than one output. If you are indenting print(total) it is part of the for loop. Do not indent it and it just output the total of all items since it will not be part of the loop.

0

Thanks for all of the helpful comments. I solved it, but I didn't use a for statement. Which to me is somehow more disappointing than not solving it at all. Here was my initial solution: cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = sum(cart) x = total - ((total * discount) / 100) print(x)

0

cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 for item in cart: total+=(item*discount/100) else: print(total) for the first case, it works but for the rest, It doesn't can anyone tell why.

0

cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = sum(cart) for item in cart: total -= (item * (discount/100)) print(total) This works for all but the 4th test case and I don't know why. Any ideas?