12/10 A day at the supermarket: stocking out


Write your code below!

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

Problem: 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


Your post does not appear to be formatted correctly, please make sure that your problem can be reproduced and that others don't have to guess what your code looks like in order to help you.

By assigning to stock, you are telling Python that your function has a local variable called stock. But you use it before assigning to it, hence your error. You should not be assigning to that name, instead it should always be referring to the same dictionary which you would do operations on so that it is modified (as opposed to replaced)


Can u pls explain in a more simplified manner?



This topic is very old, if you still have a problem, go here, press the new topic button in the top right corner, fill in the provided template so we can help you