Something of Value


#1

Hello! I have a problem with my code, it prints "It looks like your code did not print the correct total."

Already tried some combinations that I saw here, still getting the same error.

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]

def full_price(food):
    total = 0
    for x in food:
        if stock[x] > 0:
            x = prices[x]    
            stock[x] = x - 1
            total += x           
            print total
        else:
            print 'out of stock'

    print total


#2

Hey guys, I'm still struggling on this one... I am not sure where I'm wrong, it's probably the math, I've already checked and rechecked with 2 similar codes... Still getting the same error... I forgot to add the link to the problem on the OP.

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


#3

You are going a bit off the track there. In your code, x is going to be a food item, like a banana, doing math with it is not helping :slight_smile:

Instead of trying to loop and subtract every food item individually, because how long would that take if you had a zillion apples, try going at the problem like the exercise suggests;

For each key in prices, multiply the number in prices by the number in stock.


#9

This is how I did mine and it worked!

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:
inventory = (prices[key]*stock[key])
total = inventory + total
print total


#10

Would you mind explaining the math? I apologize I'm not very good at it.
I'm still struggling with this problem


#11

Daaaaaaaaaamn... it worked, thanks :smiley:


#12

I did it like this, one less line of code :slight_smile:
No idea about the pros and cons of doing either though.

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

#13

This helped! Thanks, I've been stuck for some time!


#15