Stocking out


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

Can someone tell me what I'm doing wrong? The error says that the function is returning 14 instead of 7.



Stocking out

a function ends the moment a return keyword is reached. Currently your return the stock of the last item, you should return total


Gotcha, so I should only really return once per "for" statement? or really once per function? Just trying to understand the concepts and such.


By default a function returns None, so if we want to return something different, we can use return value, value being what you want to return, in this case total, after the loop. Not the number of a item in a stock

placing return in the loop will break the loop, you most certainly don't want this. The whole loop needs to run to calculate the correct total


I need help on this... :weary:


Lets say you are in my position, and someone posted this, how would you help this person? This person provided no details about what (s)he does and doesn't understand, no code, no error message. In other words, no useful details

If you would have posted this on stackoverflow, you would have been shot down

If you want help, please go here, make a new topic, fill in the templates so we have actually details to help you


That's rude but I really do need help on this lesson


And how do you expect help with the information you provided?


I got past it nvm :rolling_eyes:


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