8.1 Keeping track of the Produce


#1

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]

Oops, try again. Check what your code prints for orange. It doesn't look quite right!

Can someone spot for me what's wrong with my code please?


#2

Look at the example in the instructions and compare to what you printed. If you still can't tell the difference then paste both the example from the instructions and the output of your code which will either have the difference in it or say that you're comparing the wrong things to each other


#3

Thanks for the advice. It worked but still can't tell what was wrong!!!


#4

Then perhaps you'll want to go back to the code you posted above and try to fix it again, this time keeping track of what you're changing and in particular paying attention to what clues you should have been following to figure it out in a more straight-forward manner.
Roughly that'll be:

problem: A and B are different
solution: compare A to B and eliminate the difference

And also worth noting is that if you don't see the difference by eye then find an exact method of comparison such as a diff tool (programs that compare one text to another) or just getting more systematic about that eyeball-comparison


#5

In the second print your stock is in capitals, it shouldn't be. I know it sounds dumb, but this is the case, computers are dumb after all, they just think they're smart.
So it would be:

print "stock: %s" % stock[key]

#6

2 posts were split to a new topic: Keeping trace of product


#7

Thanks,My eyes just couldn't spot it....


#8