A Day at the Supermarket: Something of Value


#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={}
for key in prices:
total=prices[key]*stock[key]

print total_m


I tried:
total=45+48+24+0
But it does not work.
But if I add a "_m" after the total, the result soon becomes valid?

What is the ideal answer to this questions?

Thanks


#2

total should be an integer, why is it a dictionary?

this:

total=prices[key]*stock[key]

will re-assign total everytime, instead you should add prices[key]*stock[key] to what is already in total by doing:

total = total + ...

#3

But I tried: total=total+prices["fruitname"]*stock["fruitname"].....
But the system said that I should not include the fruit names in the page.

Yeah I agree that the total should be an int, but I don't understand why the code above is valid for the question.


#4

why did you change from key to fruitname?

i suspect its a corner case, sometimes invalid code slips through


#5

Oh I just mean the meaning, not really "fruitname".
I typed "apple", "banana" and so on. And the system told me not to use fruits' names.
If I cannot type each fruit name after the prices and the stock, how can I add all the prices[key]*stock[key] together?


#6

i already told you this?


#7

Thank you, but I don't really understand what you mean...


#8

this:

total=prices[key]*stock[key]

will assign a value to total, but in doing so it will overwrite what is already stored in total. so we want to add prices[key]*stock[key] to what is already in total, we can achieve this by adding doing:

total = total + ...

simply mix total in the mix


#9

Does the ... have special meanings?
do you mean: total = total + ...
or do you mean: total=total+prices[key]*stock[key]


#10

woohoo, you understand :slight_smile:


#11

hahaha, Thank you very much!


#12