Making a Purchase help


#1



error: Oops, try again. compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +: 'int' and 'str'


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 item in stock:
        total = total + item
    return total

Thanks to anyone that helps!


#2

You only have one mistake, :slight_smile:

You don't want to add item to total, because item is a string, (that's what your error is about), but you want to add prices[item] to total. :slight_smile:
Hope this helps.


#4

Oops, too late again.


#5

You shouldn't have deleted yours, it was right too, :slight_smile: it's okay to have more than one, when people see it, different wording might help different people.


#6

Hello! Its now giving me a different error :frowning:. Thanks for helping!


#7

What error are you getting?


#8

Sorry, forgot to add, error: Oops, try again. compute_bill(['apple']) returned 10.5 instead of 2


#9

Oh sorry, you had one more issue, it's your for loop.

Instead of for item in stock, you need to use your argument instead of the dictionary 'stock'. Your argument is 'food'

so it should simply be

for item in food:

#10

Here is the correct code

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

#11

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