# Supermarket 9

#1

Hello to you all once again I have some code that is just broken and i need help fixing it because I don't know how it wants me to do it, this will be kinda sloppy but here it is:

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:
prices[key] * stock[key]
print key
total += key

print total

Yea the problem is that I don't know how they want me to multiply the key in prices to those in stock then print it when it isn't under a variable or am I reading this wrong?

#2

You should use a variable to keep the multiply result. and sum the results as your total

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

#3

Hello .First it says that you have to multiple prices key and stock key and then add its result to the variable made (total) ; in order to do that you should create a variable (any name you want ) and set it to the multiplication then add it to total.

hope it helps:)

#4

Actually, you can put them all into one 'for' loop.

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

#5

Why wouldn't this work after setting total to 0:?

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

Isn't that the same thing? Total would equal 117 instead of having to add 117 to total.

#6

Actually, you don't have to multiply and then add. You can do it all in one step, as I've done, with a success outcome. By chance it came to me as a more efficient way of putting it all together.

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

print total

This way you get the cost of the items and add them to the total all at once.

#7

That's what I was going for.. I almost had it!

#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.