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)

27th Mar 2021, 4:52 PM
Zach Z
17 Réponses
+ 8
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.
27th Mar 2021, 5:18 PM
Russ
Russ - avatar
+ 7
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)
3rd Jun 2021, 1:30 PM
Ronnie
Ronnie - avatar
+ 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.
27th Mar 2021, 5:16 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 4
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.
27th Mar 2021, 5:24 PM
Russ
Russ - avatar
+ 2
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)
27th Mar 2021, 5:22 PM
Zach Z
+ 2
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!
24th Apr 2021, 1:01 PM
Walnut
+ 1
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.
11th Nov 2021, 8:06 AM
Daniel Branton
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
30th Jun 2021, 12:59 PM
Kuriboh
0
cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = sum(cart) for item in cart: total -= item * discount/100 print(total)
21st Oct 2021, 3:18 PM
nisha nambaiya
nisha nambaiya - avatar
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)
15th Nov 2021, 5:41 PM
William Kinkead
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.
20th Nov 2021, 5:32 PM
Dev Priya Saha
Dev Priya Saha - avatar
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?
30th Nov 2021, 3:06 PM
Sarah Aardwolf
0
cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 for i in cart: total += i print (total-(total*discount/100)) #this works just fine for all
6th Dec 2021, 5:57 PM
fatoliman
0
what i did is that i declared a different variable for item - (item * discount/100)} part as amount i.e. cart = [15, 42, 120, 9, 5, 380] dis = float(input("Enter Discount")) amount = 0 total = 0 for item in shoplist: amount = item - discount/100 * item total += amount print(total) and it worked fine, so hope it helps.
6th Jan 2022, 12:38 PM
Khushi Sharma
0
cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total= sum(cart) a = total-((total*discount/100)) print(a)
6th Jun 2022, 9:39 PM
Abdul rahmon
Abdul rahmon - avatar
0
cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 for x in cart: total += x-(x*discount/100) print(total)
4th Aug 2022, 2:12 PM
Abdul Ghafar
Abdul Ghafar - avatar
0
You can try this cart = [15, 42, 120, 9, 5, 380] x = float(input()) total = 0 for cart in cart: calc = cart - (cart * x/100) total += calc print(total)
14th Oct 2023, 4:17 AM
S Dhivya Bharkavi