Something of Value


#1



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


what am i doing wrong? it says that it prints the wrong value


Replace this line with your 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 = prices[key] * stock[key]
print total


#2

here:

total = prices[key] * stock[key]

a single equal sign means assign, overwriting the value already stored in total. You should add prices[key] * stock[key] to what is already stored in total


#3

Hello stetim94, I have also a problem in this unit.

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

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

I do not know where am I wrong.
I will appreciate all assistance or suggestions

Thanks!


#4

two equal signs means check if equal, why would you use that here? You want to assign total a new value (single equal sign) so you calculate the total cost of all your products in stock


#6

Thanks for getting this started.
Took me a while, but then realised I needed a subtotal.

If I understand it correctly, it is looping through the list.
Instead of starting with total = , start with subtotal = .

Finish off with the total adding in the previous subtotal.

My solution is below:

total = 0
for key in prices:
subtotal = prices[key] * stock[key]
total = total + subtotal

print total


#7

thank you, helped a lot


#8

you're welcome :slight_smile: Did you figure it out completely? If you need more help, post an updated version of your code


#9

thanks man worked
really awesome


#11

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