A day at supermarket ( Error: list indices must be integers)

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

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
   "pear": 15

prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3

# Write your code below!
def compute_bill(food):
    total = 0
    for fkey in food:
        for skey in stock:
            if fkey == skey and stock[skey] >= food[fkey]:
                stock[skey] -= food[fkey]
                total += (food[fkey] * prices[skey])
    return total

print(compute_bill({'banana': 7, 'apple': 2}))

for skey in stock:
    print(skey + ':' + str(stock[skey]))

I run the code in my pycharm environment ant it working without any error but when I try to run the code inside the tutorial it gives me "list indices must be integers" error.


Did you test all the things that your code is supposed to carry out? Sounds like codecademy tested something that you didn't.

If the error message is in a red banner then it might also be telling you what it was trying to do which gives you a very easy way of finding out what was tested when your code crashed.


The result of code is correct, I pass the same parameter that codecademy use to test the code.
I debug the code in pycharm I don't think there is any error.


This is the parameters codecademy sent to method
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


There's nothing in your code that matches calling compute_bill with that input though, so are you really testing the same thing?

That it behaves the way you expect for some particular input isn't proof that it behaves correctly for all input or that you even gave it valid input

That says how you should test your function to reproduce the crash


This is how I test the code :
compute_bill({'banana': 1, 'apple': 1, 'pear': 1})

Can you fix the code please


That's not how the instructions describe the input format, it's not a list. If that's the input you're using, then you're testing something other than what they mention the crash occurs for


So Why it not crash in pycharm and I check the list section, could not find any way to send the list with key and value ['key' : 'value', ...]


I believe we already figured that out. You didn't try the same thing as codecademy did when it tested your code.

Do you have any indication of that the input to your function should be key-value pairs? Have another look at how the input is supposed to be structured


Ok I'll read the list and dictionary part again, but I think sending a dictionary is much simple.


I change the code, try to send a list and it pass

    def compute_bill(food):
        total = 0
        for fkey in food:
            for skey in stock:
                if fkey == skey and stock[skey] >= 1:
                    stock[skey] -= 1
                    total += (1 * prices[skey])
        return total


Shouldn't you just be looping through the list? What does the other loop do? Make sure that you can motivate what each thing does, otherwise it should probably be removed

Yes, an amount of groceries could also be represented as a dictionary, there are advantages and disadvantages to both of them and any other representation. The task at hand though, is "given this input, produce this output"

And also, keeping track of other involved interfaces and how they behave is important (in this case the submission test which both provides input and checks output)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.