9/13 Whats wrong?


#1

prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}

for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]

total = 0
for key in prices:
total = prices[key]*stock[key]
print total

and it says: It looks like your code did not print the correct total


#2

The problem is in this line:

total = prices[key]*stock[key]

In the every iteration of the loop, you set variable total to the new value. In this way after the loop your total value stores only result of the last calculation.

You should use += or + operator here :wink:


#3

Well, it works but I don't know why I can't use just =. In this case you just need to calculate price. Not store all calculations.

Sorry I'm just begginer.


#4

Don't worry, I am here to help you.

In this exercise, you want to calculate a total price of products in the shop / basket.

The price of every product is stored in prices list and number of products in stock.

You created a loop that iterates through every product. Expression prices[key] * stock[key] calculates total price of product key.

Let's execute your loop line by line:

for key in prices:
    total = prices[key] * stock[key]
    print total

Iteration #1

key = "orange"
prices[key] = 1.5
stock[key] = 32

total = 1.5 * 32 = 48.0

Iteration #2

key = "pear"
prices[key] = 3
stock[key] = 15

total = 3 * 15 = 45

Iteration #3

key = "banana"
prices[key] = 4
stock[key] = 6

total = 4 * 6 = 24

Iteration #4

key = "apple"
prices[key] = 2
stock[key] = 0

total = 2 * 0 = 0

Do you see a problem? Your total variable does not store price of all elements. It only represents price of the last element, because with every iteration you overwrite previous value.


#5

Ah, I get it now. Thank you very much. :smile:


#6

You're very welcome :smile:


#8

This is my code, which worked. Orange was giving me problems because it is a float. I removed the plurals for this and the last exercise to get through.

price = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}

for key in price:
print key
print "price: %s" % price[key]
print "stock: %s" % stock[key]
total = 0
total = stock[key] * price[key]
print total


#9

it's not working bro its my code

prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}
for item in prices:
print item
print "price: %s" % prices[item]
print "stock: %s" % stock[item]
total=0
total= prices[item]*stock[item]
print total


#10

I'm having the same problem as you designace. Did it work for you when you added "for x in prices." above?

total = 0
for x in prices:
total += prices[x] * stock[x]
print total

Make sure to indent the total lines. However, I'm still confused why we have to add in for x in prices. Is that the for loop? How do we know when to add in the for loop in code?


#11

can you show the full correct code


#12

in pyth

prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}

for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]

total = 0
for key in prices:
total += prices[key] * stock[key]
print total


#13

This is the correct code guys:

prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}

for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]
x=prices[key]*stock[key]
print x
total = 0
for key in prices:
total = total + (prices[key]*stock[key])
print total


#14

:smile: You are really good at explanations, i'v never seen anyone explain anything like so.


#15

I see this is as a good answer without the need of declaring an 'x' variable:

prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}

for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]

total=0

for key in prices:
total = total + (prices[key]*stock[key])
print total


#16

You need to use the += modifier in your line total = prices[item] * stock[item]. This adds the value of prices[item] * stock[item] to the existing value of total, instead of just overwriting the value - what the = does.


#17

guys, this piece of code passes the exercise, but it prints out some weird values. What it prints out is:
48.0
93.0
117.0
117.0
which is clearly wrong, yet for some reason the exercise is a success. With "=", as in "total = prices[key]*stock[key], the values are correct, but the exercise is failed. With "==" it prints out zeroes and the exercise is failed as well.
So what is going on?


#18

thank you your code really helped


#19

Thank you very much for your kind words! :smile:

I am trying my best!


#20

This is my working code:

satotal (sold all total)

prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}

for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]

total = 0
for key in prices:
satotal = prices[key]*stock[key]
print satotal
total = total + satotal
print total


#21

Does your code look like this? I had the same thing

total = 0
for key in prices:
    total += prices[key]*stock[key]    
        print total

When I removed the indentation of print total this cleared it up:

total = 0
for key in prices:
    total += prices[key]*stock[key]    
print total