"Making a Purchase" Dictionary or list?


The code below counts tallies the prices of the fruit in the {prices} dictionary and ouputs the the sum just fine:

shopping_list = ["banana", "orange", "apple"]

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

def compute_bill(food):
    for key in food:
        total = (prices[key]) + total

    return total

fruitcost = compute_bill(prices)
print (fruitcost)

But my question is: Was that even the assignment? Doesn't Code Academy want us to instead tally the cost of the shopping_list = ["banana", "orange", "apple"] ??

It's on the first line. Of course it is not a dictionary with key/value pairs but rather a simple list. I figure they MUST want us to use that because otherwise why make us create the list in the first place?

Lastly, if that is indeed what they want, it seems like it should be do-able but I can't for the life of me figure out how to do it. Help?




You will have to figure out a way to::

take an "item" from the shopping_list
check if you have the "item" in stock dictionary and if it is greater than 0 (zero)
and if so...
=add= the corresponding number Value found in in the prices dictionary
to the variable total


Hi @ronnumber ,

If you want to tally the cost of shopping_list, you can do this ...

print compute_bill(shopping_list)

When you get to Exercise 12: Stocking Out, however, Codecademy will object if you call the compute_bill function. But you can call it in Exercise 13: Let's Check Out! without a problem.


I see where you're going with this, but for this exercise, Code Academy says "Ignore whether or not the item you're billing for is in stock."

It would be interesting to try what you're saying--maybe later!


Thanks that worked! Don't know why I didn't think to try that--it's a combination of over-thinking it and underestimating
what the code can do I think.


Hi @ronnumber ,

Yes, for this exercise, you should ignore whether or not the item is in stock. But, @leonhard_wettengmx_n is correct regarding what ultimately needs to be done, and that will be addressed in the later exercises.


Thanks I shall push on