# compute_bill(['apple']) resulted in a TypeError: list indices must be integers, not str

#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 i in food: morefood = food[i] total = total + morefood return total```
I keep on getting compute_bill(['apple']) resulted in a TypeError: list indices must be integers, not str

#2

``````def compute_bill(food):
total = 0
for item in food:
total +=prices[item]

the problem is when you say

`` for item in food:``

the interpreter creates an iterator called item over the elements in your list or dictionary. The iterator will have the same type as your element and since your elements are string then item is also a string. You can only access list elements by indicies thats why you get an error

Making a purchase code error
#3

Thank you. Good explanation

#4

But it makes no sense to get the error, ['apple'] is never used, i changed the code to only use a test list and I still get the error?

# Write your code below!

def compute_bill(food):
total = 0
print food
for n in food:
print n
total += n
an = [2,1,3,4]
print compute_bill(an)

#5

your list doesn't have an item called `"apple"` in it

#6

Hi Rydan,

Thanks for posting the code. I have a question: are dictionaries treated as global? Because my intuitive approach was not to use prices in the function since it is not defined inside nor passed as argument.

Regards,
Juan

#7

#8

cool, thank! @leonhard_wettengmx_n