Making a purcahse


#1

I'm having trouble on "Making a Purchase" of "A Day at the Supermarket".

It keeps saying that compute_bill(['apple']) returned 1 instead of 2.

Here is my code:

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

I feel like I made a really obvious mistake...


#2

Lets add a function call so we can see what is actually happening:

def compute_bill(food):
    total = 0
    total = total + len(food)
    return total
print compute_bill(shopping_list)

important: remove the function call once your done, otherwise you will an error message. But it is nice to have the function call while working on the code

so food is a list containing the products we want to buy. So we need a loop, to loop over food, then we need to get from product to price (maybe our prices dictionary can help us with this?)

Then we need to add the price to total, then after the loop, we need return total

remove the function call


#3

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