Stocking Out! PLZZ HELP


#1

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.

PLZZ HELP WITH THIS AS SOON AS POSSIBLE
THANKING YOU IN ADVANCE

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):
total=0
for x in food:
if stock[x]>0:
if stock[x]>food[x]:
total+=prices[x]*food[x]
stock[x]-=food[x]
else:
total+=prices[x]*stock[x]
stock[x]=0
food[x]-=stock[x]
return total


#2

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:


#3

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


#4

Oh I understand!

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


#5

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.....


#6

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


#7

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


#8

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


#9

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.


#10

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


#11

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


#12

By the way ,thanks for all your your help.


#13

No problem! Enjoy Python!


#14

NO...Thats the only trouble


#15

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


#16

The above mentioned error message appears everytime


#17

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


#18

Ok. Thanks for your help.


#19

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:


#20

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]
            stock[x]-=1
    return total