# List indices must be integers, not str - SOLVED

#1

Stocking Out

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

the compute_bill function should not return error

``````def compute_bill(x):
total = 0
for item in x:
if stock[item] > 0:
print x[item]
total += x[item]
stock[item] -= 1

#2

`total += x[item]`

There is one mistake!
total is a number data type but x[item] is a string

#3

#4

I'll give you a hint..

``````def compute_bill(x):
total = 0
for item in x:
if stock[item] > 0:
print x[item]
total += x[item]
stock[item] -= 1

Analysis of this code....

1.We define a function compute_bill(x) that takes a list i.e x.
2.We define a local variable total and set it to 0, it will keep track of our total amount.
3.We loop through this x list.
4.If this item of x list (fruits) is in stock (greater than 0 in numbers)

# You're doing mistake in fifth step.

5.Then we add price of fruit to total. <-- You have to access Prices dictionary!
6. As we buy fruits from stock, the number of fruits will be get decreased by 1.
(As we're buying one item of every fruit so we will subtract 1 every time we buy(when looping) fruit )

I hope it helps!

#5

I have already tried accessing the prices dictionary and happen to get the same error again.

Here is the snippet below:
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(x):
total = 0
for item in x:
if stock[item] > 0:
print x[item]
total += prices[item]
stock[item] -= 1

#6

What error you're getting?

#7

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

#8

You have to remove this... as Codecademy editors does not want it as output! (Your output should match with codecademy's output)
updated** @spoorthi_k_a
`print x[item]` # no where mentioned to print the fruit name

#9

Hey Thanks a lot. On erasing the print statement it did work.
Its strange to see such an error coz of print statement

#11

Actually as I've mentioned , your code's o/p wasn't matching with Codecademy's o/p so It created the error!
Keep up the good work!!

#12

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