Stocking Out! PLZZ HELP


My code is calculating the right answer. But Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: list indices must be integers, not str
This error message appears every time but on console i get the right answer every time.


My code is as follows:

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 x in food:
if stock[x]>0:
if stock[x]>food[x]:
return total


When you loop through food, you're getting the key, which is a string, not the value, which is an int. Adjust your code to make it reference the value, instead of the key. :smiley:


Actually, with reference to my code,x is the key,and i m using it like stock[x] which is the whats wrong...
By the way thank you for your prompt answer.
But am still not able to get it completely


Oh I understand!

In that case, I'm assuming you're calling either stock or prices as food?


No, actually, I m using x to address the corresponding keys, i.e. Banana apple etc.
And as all 3 dictionaries have the same keys, so i can easily use my x to obtain the key values.....


Correct. I mean when you call your function, you're including stock or prices as the parameter?


And by doing this i m always addressing the values and not why that error..


Ok.....its stock of what we have to buy that is....the dictionary food is the parameter


As far as I can tell... x is being used as the KEY, rather than the value. Try reevaluating that code, to reference x's value.


But my code is evaluating correctly every time. You can run it by yourself if you can Sir by now.


And so it is... Does it allow you to pass?


By the way ,thanks for all your your help.


No problem! Enjoy Python!


NO...Thats the only trouble


If it evaluates correctly outside... That sounds like yet another bug. I'd recommend reporting it. Sorry about that.


The above mentioned error message appears everytime


But if you run it outside of codecademy, it works?


Ok. Thanks for your help.


Not sure. I can't run it now, as i m learning through this interface, and have no idea how to run it outside.
But if you can, then plzz let me know :slight_smile:


Taking a look...

Your code does not actually run at all. I'm not sure what the issue is, but take a look at my code, and see if you can figure it out:

def compute_bill(food):
    total = 0
    for x in food:
        if stock[x]>0:
            total += prices[x]
    return total