0

The shopping cart is declared as a list of prices, and you need to add functionality to apply a discount and output the total pr

cart = [15, 42, 120, 9, 5,380] discount = int(input()) total = 0 for y in cart: total = y - (y*discount/100) total += total; Print (total)

12th Sep 2021, 8:40 AM
Virendra Agarkar
Virendra Agarkar - avatar
14 Réponses
0
Virendra Agarkar total += total means total = total + total = 2 * total which is wrong and gives wrong output
12th Sep 2021, 8:59 AM
A͢J
A͢J - avatar
+ 2
Virendra Agarkar Ok then if you want to use loop and calculate individual discount then do this: cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 for y in cart: total += y - y * discount / 100 print (total)
12th Sep 2021, 8:54 AM
A͢J
A͢J - avatar
+ 1
Why we have to use '+=' in code not 'total += total'
12th Sep 2021, 8:57 AM
Virendra Agarkar
Virendra Agarkar - avatar
+ 1
Thanks i got it
12th Sep 2021, 9:05 AM
Virendra Agarkar
Virendra Agarkar - avatar
0
Virendra Agarkar Why did you do 2 times -y?
12th Sep 2021, 8:42 AM
A͢J
A͢J - avatar
0
Virendra Agarkar First get total from the list cart then get discounted amount from total using this formula. Y - (Y*X/100) where Y = total, X = discount
12th Sep 2021, 8:44 AM
A͢J
A͢J - avatar
0
How?
12th Sep 2021, 8:45 AM
Virendra Agarkar
Virendra Agarkar - avatar
0
Virendra Agarkar Like this using sum function get total then calculate discount. cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = sum(cart) print(total - total * discount / 100)
12th Sep 2021, 8:46 AM
A͢J
A͢J - avatar
0
But we have to take total of each element discount and then take total it.
12th Sep 2021, 8:48 AM
Virendra Agarkar
Virendra Agarkar - avatar
0
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.
12th Sep 2021, 8:50 AM
Virendra Agarkar
Virendra Agarkar - avatar
0
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)
12th Sep 2021, 8:51 AM
Virendra Agarkar
Virendra Agarkar - avatar
0
Virendra Agarkar Instead of taking individual discount and then add them we can add all amount then get discount, both will give same result.
12th Sep 2021, 8:51 AM
A͢J
A͢J - avatar
0
But how to do it i am unable to write perfect code
12th Sep 2021, 8:53 AM
Virendra Agarkar
Virendra Agarkar - avatar
0
cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 i=0 for x in cart: total = total+(cart[i] - (cart[i]*discount/100)) i=i+1 print(total)
28th Oct 2021, 11:57 AM
Евгений Турицын
Евгений Турицын - avatar