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)
17 odpowiedzi
+ 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.
+ 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)
+ 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.
+ 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.
+ 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)
+ 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!
+ 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.
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
0
cart = [15, 42, 120, 9, 5, 380]
discount = int(input())
total = sum(cart)
for item in cart:
total -= item * discount/100
print(total)
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)
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.
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?
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
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.
0
cart = [15, 42, 120, 9, 5, 380]
discount = int(input())
total= sum(cart)
a = total-((total*discount/100))
print(a)
0
cart = [15, 42, 120, 9, 5, 380]
discount = int(input())
total = 0
for x in cart:
total += x-(x*discount/100)
print(total)
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)