Stocking Out


#1




The system keeps on returns :
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


Please help me check the mistake in this code.


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


#2

you need to decrement stock[item], not stock.
so

stock[item] = stock[item] - 1

or even better

stock[item] -= 1

#3

Thank you, that's the solution.


#4

Can you please me check out my other problem with 'Iterating over a list in a function', that will be really helpful.


#5

but whay we use total=total+prices[item]
.................................^^^^why we use this total to ad again in prices[tem]


#6

Because on the instruction it says: add the price of the item to total.
So it came clear that this should be the way to add it.


#7

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