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,
}
total = 0
for key in prices:
total+= prices[key] * stock[key]
print total


but I got a answer like this:
48.0
93.0
117.0
117.0

I don’t understand why comes out 4 numbers?and why 48 and 93 will come out? I thought the right answer should just be 117, so is there anything wrong with my code?


#2

you print total each iteration of the loop, so 48 is the oranges (32 * 1.5) and so on

if you only want the final result, place print total outside the loop


#3

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