Something of value


#1

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

I copied this from someone. Why "I" instead of another letter like "x" or "n"?


#2

There's no reason, i is just a usual convention for "item".


#3

Below is a correct answer

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]

for key in price:
total = price[key] * stock[key]

print total


#4

I tried that but it doesn't seem to work for me, I find the error at the end. Your printing the starting value of total instead of returning the calculated value. But even as i try to return it i get an error that says my return is outside the function??


#5

You don't need to use return. Try this, it worked for me.

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:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]
total = total + (prices[key] * stock[key])

print total


#6

This is my code

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


#7

is it help you to answer ?


#8

yes, it worked for me.and passed.