Something of Value


can any one tell me where is the bug in this program. i had get only one correct answer. for apples only.
when i hit on save&submit it shows me like this.
total = 0
for price in prices:
total = stock[key] * prices[price]

print total

Oops, try again. It looks like your code did not print the correct total.


@hchirasani ,

What are the roles of price and prices in this line ...

for price in prices:

... and how should that information be used in the line that follows?


Hi, as we all know that to call strings in dictionaries, list we use to write for loop as follows

For example:
flowers = [ "Rose", "lily" , "Jasmine", "Hibiscus"]

To print all the flowers in the list we have to write For loop as follows.

for string in flowers:
print flowers

like above programming. to multiply prices with stock, so wrote for loop as follows

for price in prices:

is there any wrong in writing this.


This is correct ...

for price in prices:

... but this is not ...

total = stock[key] * prices[price]


There are 2 mistakes.

  1. you should loop through the prices by price in the loop and no mixed by key and price!!
  2. Add all prices to the variable total by "total += stock[price] * prices[price]"

Correct code:

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


Try this 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 i in prices:
mul = prices[i] * stock[i]
print mul
total = total + mul
print total
The reason it might work is because, the total should be placed in the for loop to print the correct total
else it will print only the total of 0 and the last multiplied value
Apperently there are many ways of solving it


i did not get the correct values when using this code? I'm not really sure what is causing this.


the correct program is as follows.
for price in prices:
total += (stock[price] * prices[price])
print (total)
print (total)