9/13 I don't know what I'm doing wrong


#1

My 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] + total
        print key
        print total

I don't know what I'm doing wrong.
I'm thankful for any help


#2

Me neither. Describing what's happening and what you want to happen instead is a good start.
If we're to read your code, then it would be advantageous if it was intact (try copying your code and running it, notice that it complains about indentation?)


#3

you indented your "print total" line. It is supposed to go after your loop. That should fix it!


#4

Hi @dunklergecko, please always format your code. Thanks! :slightly_smiling:

The problem is that the indentation after the line print "stock: %s" % stock[key] in:

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] + total
        print key
        print total

is way off. The total and loop for key in prices need to be outside the loop for key in prices. Also, the last two print's indentation seems a little too towards right.


#5

Thanks for the help, it finally worked. :smiley: