0

Guys how I can sum this on the end?

#This is th code: cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 i=0 for total in cart: total= total-(total*discount/100) print(total) I want "total" sum all of them. Thanks

25th Jun 2021, 12:28 PM
Bryan Buenaver
Bryan Buenaver - avatar
7 Respostas
+ 3
Do you mean to sum all the cart's item after giving discount? If it is, then you can do like this: https://code.sololearn.com/ca119a20a10a You can also use the map() function to do this: https://code.sololearn.com/c6A13A5776a2
25th Jun 2021, 12:36 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 2
total = sum(cart) discount_num = total*discount/100 gtotal = total - discount_num print(gtotal, discount_num)
25th Jun 2021, 1:16 PM
Shadoff
Shadoff - avatar
0
https://code.sololearn.com/ca185A11A10A cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 i=0 for a in cart: total= total+a total=total-(total*discount)/100 print(total)
25th Jun 2021, 12:54 PM
Abhishek Kumar
Abhishek Kumar - avatar
0
You wrote : for total in cart, thats wrong. You have to use for i in cart: i are the numbers in the list cart. You can it named like you want, x,y or what ever. Then look at the formula: total += i + (i*discount/100) Do you understand that, if not please ask again. cart = [15, 42, 120, 9, 5, 380] discount = int(input()) total = 0 #i = 0 for i in cart: total += i + (i*discount/100) print(total)
25th Jun 2021, 1:06 PM
Angela
Angela - avatar
0
One Liner : dis = int(input('Enter Discount : ')); res = round(sum([round(i-(i*(dis/100)),5) for i in [15,42,120,9,5,380]]),5); print(res) Here I have compressed the expression using only two variables. The dis var is used to take the input from user. The res uses list comprehension to retrieve a new list of discounted prices rounded and then with the sum() build-in funtion is found the sum of this list, surely rounded. Other ideas were great, but you can do it short and easy 😄. However you can understand this code by breaking up to different variables 😀.
25th Jun 2021, 10:03 PM
Ervis Meta
Ervis Meta - avatar
0
cart = [15, 42, 120, 9, 5, 380] dis = int(input()) total = 0 i=0 sum = "" for t in cart: t -= round(t*dis/100) sum += str(t) print(sum)
26th Jun 2021, 10:42 PM
Alumona Benaiah
Alumona Benaiah - avatar
0
Here is the correction in your code : ----------------------Code----------- for i in cart: total = total + (i -(i*discount/100)) print (total) -----x-------x-------- This will show the net total. Your mistake : you used total variable to iterate through cart. But we need to use any other variable for iteration only so it doesn't affect the net total value. How it works : We used i to iterate through cart. In each iteration we added the discounted price to the total. So in the end after the loop we have the net sum ( total ) of all items in cart. And so we printed the value at the end.
27th Jun 2021, 7:29 AM
Rahul Kumar
Rahul Kumar - avatar