Stocking Out #12


#1

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: local variable 'stock' referenced before assignment. I have no idea what this means. All I did was follow the directions.


def compute_bill(food):
    total = 0
    for key in food:
        if stock >=0:
            total += prices[key]
            stock -= 1
    return total


#2

@morganw01 The error came from the use of only stock instead of stock[key]
This code will work perfectly::slightly_smiling:

  def compute_bill(food):
            total = 0
            for key in food:
                if stock[key] > 0:
                    total += prices[key]
                    stock[key]-= 1
            return total

#4

Hey xtrimzz,
I just saw your comment, and the only difference between your (working) code and mine (not working) one seems to be that you put a double indentation in your second line before "total", and I only put one there.
Can you explain why you chose a double one?


#5

For this and the last couple exercises, I've been getting the code right but in the upper right hand corner, it prints "none". I should be getting a value, right? Even if I have been printing it?


#6

Hello @tagace43233,
I think it doesn't matter whether the indentation is single. I might as well use single indentation. The difference is one line 4 "if stock >= 0" instead of "if stock[key] > 0 "

def compute_bill(food):
    total = 0
    for key in food:
        if stock[key] > 0:
            total += prices[key]
            stock[key]-= 1
    return total

Hope it helps! :slightly_smiling:


#7

Hi @tyriverag,
You should get a value when you call the function on print .
print compute_bill(food).
Hope it helps! happy coding!