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)
14 Réponses
0
Virendra Agarkar
total += total means total = total + total = 2 * total which is wrong and gives wrong output
+ 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)
+ 1
Why we have to use '+=' in code not 'total += total'
+ 1
Thanks i got it
0
Virendra Agarkar
Why did you do 2 times -y?
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
0
How?
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)
0
But we have to take total of each element discount and then take total it.
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.
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)
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.
0
But how to do it i am unable to write perfect code
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)