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


#1
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}))

print('stock')
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.


#2

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.


#3

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.


#4

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


#5

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


#6

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

Can you fix the code please


#7

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


#8

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', ...]


#9

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


#10

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


#11

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

#12

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)


#13

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