#defines the function
def compute_bill(food):
total = 0
for item in food: #loops through each item
if stock[item] > 0:
# if the stock is more than 0 then add price of item to total and minus 1 from stock
total = prices[item] + total
stocks[item] -= 1
return total
You have a dictionary named stock. Make sure that within the for loop you refer to it by that name, using the exact same spelling wherever you attempt to access it.
Since your posted code is not formatted, users cannot see its indentation, making it difficult to help. Does the error message specify a line number where the problem occurred?
@bisharp, the problem may be that you used both spaces and tabs for indentation. In a Python program, you should use either spaces or tabs, and not both. On Codecademy exercise pages, uses spaces for indentation.
If you used a tab for indentation on one or both of these lines …
total = prices[item] + total
stocks[item] -= 1
… then after you format your code, the indentation might not look the same as it did in the Codecademy editing window. To correct a problem with a tab in indentation, you can remove all the indentation from the problematic line, then redo the indentation, using only spaces.
We assume you have already corrected the problem with the dictionary name here …
Following is your original compute_bill function, formatted …
def compute_bill(food):
total = 0
for item in food:
#loops through each item
if stock[item] > 0:
# if the stock is more than 0 then add price of item to total and minus 1 from stock
total = prices[item] + total
stocks[item] -= 1
return total
Note that the indentation of these two lines looks strange …
total = prices[item] + total
stocks[item] -= 1
That is because the indentation of the first of those lines consists of four spaces followed by a tab. You can confirm that by gradually selecting the whitespace at the beginning of the line.
Instead, the indentation should consist of six spaces, as is the case with the second of those two lines. The indentation of those two lines must match.