Stocking out


Stocking Out

Error message: Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7

please help, from looking at the other posts for this exercise I can't work out what's wrong. The stock does not seem to decrease?

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

print stock


a function ends the moment a return keyword is reached

so, your function ends the in the first iteration of the loop

in other words, only the price of first product (if in stock) is added to total, then return keyword is reached so the function ends

so, customer can't buy more then one product because of this mistake in your function


Thank you so much @stetim94, makes perfect sense and the tiniest change I'd have never thought of!

Much appreciated,
Jo x


Well done :slight_smile: that you know what to do to make the code work, means you understand :slight_smile: Understanding is the most important part here


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