0

Shopping cart

Hi all, I dont really understand this solution for the shopping cart problem. especially the bit where its says total = 0 and "for c in cart". where is this coming from? cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 for c in cart: total = total + (c - c*discount/100) print(total)

3rd Aug 2022, 2:20 PM
Johan Ahmed
Johan Ahmed - avatar
1 Antwort
+ 2
total is a variable which hold 0 'for c in cart' is a for loop where 'c' contains each value of list 'cart' Here you are adding discounted value to variable 'total' for loop will access each value in a iterator value 'c' which is used to get discounted value. So if you enter 5 then discounted amount will be 15 - 15 * 5 / 100 + 42 - 42 * 5 / 100 + 120 - 120 * 5 / 100 + 9 - 9 * 5 / 100 + 5 - 5 * 5 / 100 + 380 - 380 * 5 / 100 = Total
3rd Aug 2022, 3:09 PM
A͢J
A͢J - avatar