HELP A day at the supermarket 11


#1

I apologize for remaking this topic but I have had no help or at least any that works but my code looks like this:

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 
    total += food
    return total

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


#2

food is a list that we will need to iterate over to accumulate a total.

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

#3

where does prices come in?


#4

It is one of the dictionaries. stock and prices.

Given the shopping list,

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

notice that the elements correspond to key names in both the stock and the prices dictionary. In our loop above, the variable item takes on one name at a time and uses it as the look-up key in the prices dictionary.


#5

OK I understand cause you can use both dictionaries at once


#6

now it says this:Oops, try again. compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5


#7

Yes we can since their keys are the same. Be sure to use the prices, though, since those are the values we wish to total up.


#8

Please re-post your code. See if you can format it, this time.


#9

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 food:
        total += prices[item]
        return total

#10

Too much indentation. It is returning on the first iteration.

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

#11

Got it thanks man i have been stuck on this for ever


#12

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