Stuck on exercise 12 stocking out


I've been making every change possible to make this code work. I'm not trying to sound narcissistic but I believe it's correct.
The error message I've been getting reads "Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: local variable 'item' referenced before assignment". Here is my code, trying to get a reply ASAP

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

I've spent hours trying to tweak this and look at other topics on the forum, any help would be greatly appreciated :smile:


item is an iteration variable and defined on the fly while looping over food.
so you can't use it before you define it,
The if part should be inside the for loop.

Also make sure you do correct indentation.


thank you!!!!!!!!!!!!


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