11) Making a purchase



When I run my code, I get an error saying that calling compute_bill with 1 apple, 1 pear, and 1 banana result in 4 instead of 7. I don't know why it is running those specific items, why it is resulting in 4, or why it should be resulting in 7 when the total price of those items is 9. Additionally, I'm having trouble understanding the purpose of the shopping list because it isn't being used anywhere that I can. If it is, what keyword is telling the code to pull from that list and not the entire dictionary?

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):
    for i in food:
        if stock[i] > 0:
            total += prices[i]
            stock[i] = stock[i] - 1
            return total


a function ends the moment a return keyword is reached

so your function ends the moment a product is in stock, so the customer can never buy more then item from the store


So what is the solution to that?


as for your question, food (the function parameter) will be a list. function parameters are placeholder until you call the function, depending on what argument you supply on function call, what the value of food will be, so lets say we have the following function call:

print compute_bill(shopping_list)

food will now contain the shopping_list

please don't add the function call, it will throw an error

when do we want our function to end? After all the products the costumer are billed (added to total)

which is after the for loop, so return should be outside/after the loop


Thanks, I got it to work by decreasing the indent.


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