Code Not Working - A Day at the Supermarket


#1



https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096#


Error Message
"Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: string indices must be integers, not dict"


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

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 04,
    "apple": 2,
    "orange": 01.5,
    "pear": 03
}

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


#2

Your error message is saying that you're doing something like:

'hello'[{'hello': 5}]

A dictionary is not a valid index for a string. Integers are:

'hello'[2]

#3

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 key in food :
total += key
return total
it says type error :unsupported operand type for int str
whats wrong?
and whats the right way fo coding?


#4

you're getting the error because you are telling it to add an integer(total) to a string(key).


#5

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