Something of Value?


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/1/4?curriculum_id=4f89dab3d788890003000096#


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


prices = {
    "banana" : 4,
    "apple"  : 2,
    "orange" : 1.5,
    "pear"   : 3,
}
stock = {
    "banana" : 6,
    "apple"  : 0,
    "orange" : 32,
    "pear"   : 15,
}

for fruit in prices:
    print key
    print "price: %s" % prices[fruit]
    print "stock: %s" % stock[fruit]
    total = 0
    print prices[fruit] * stock[fruit]
   
print total

What did I do wrong?


#2

start with total = 0 before the for loop.

Then make your total equal to prices[fruit] * stock [fruit] inside the for loop


#3

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

it still says the same error although it is showing the totals


#4

Just to clarify, you need:
total += prices[key] * stock[key]


#5

That worked for me . Thanks!:grin:


#6

why the + and = sign together?


#7

It's so you can keep a running total of the inventory value as you progress through the loop. The first iteration totals the value of the bananas in stock. Then it goes through three more times, each time adding the value of the different fruits in stock.


#8

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

It said the line in bold has invalid syntax. Does anyone know what I did wrong?


#10

Because you have brackets on 'prices', instead of 'key'.


#13