Making Purchase-why shopping_list not used


I do not fully understand the correct code works (how does it know what ‘food’ and ‘item’ are and how does it know to refer to my shopping_list to tally the total). I originally came up with the following:

# Write your code below!
def compute_bill(food):
  total = 0
  for food in shopping_list:
    if stock[key] > 0:
    	total = total + prices[key]
      stock[key] = stock[key] - 1
			return total

The error said key was not defined, which I thought that was what you used to get it to refer to whatever was on the right side of each colon in a dictionary, so feeling confused about that as well. Don’t see anyone else with these misunderstandings so would really appreciate explanation. Thanks


look here:

def compute_bill(food):

food is the function parameter, this means that at function call we get an argument (which could be shopping_list or any other random list with items customer wants to buy)

as such, we should loop over food. This way, our function works for multiply list (we just need to supply this lists as argument at function call)

Food (function parameter) is a placeholder until its get value from argument at function call


I guess I don’t understand how it knows to go to shopping_list versus stock
or prices when all I have given it is this new word as a parameter that
doesn’t appear to point it to any specific list. I don’t understand how
’food’ and ‘item’ are suddenly magically defined, and key isn’t, when key
seemed to work just fine in a previous exercise. I am definitely doing a
lot of guessing without really comprehending…


Hi @lbierwer,

To understand why we use food, you must first understand why we use parameters.

As of defining the function, the program does not know whether food is a number, a string, a list, a dictionary, etc. As far as the program is concerned food could be anything and, well, it could care less. It’s just a placeholder for whatever you pass into the function when you actually call it. In other words think of parameters like variables. Whenever you call a function, the argument you pass into it becomes the value for food. For instance if you called:

compute_bill(["banana", "apple"]);

You would essentially be telling the program to run compute_bill(food) except now, food = ["banana", "apple"]. Until a function is actually called, the parameters you have set are valueless placeholders. Once you call the function and pass in arguments for those parameters, they take on value and purpose. What I mean by this is, if you did the following:

prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
def compute_bill(food):
    total = 0
    for n in food:
        total += prices[n]
    return total

compute_bill(["banana", "apple"])

Notice how I defined the function using food as a parameter and then I called the function using ["banana", "apple"] as an argument. This is telling the program to run the code within compute_bill(food) but replace all mentions of food with ["banana", "apple"]. Basically it does this:

total = 0
for n in ["banana", "apple"]:
    total += prices[n]
return total

This is why we use food as we do in the function.


Thanks for the explanation. I am starting to understand. Anyone else that
wants to pipe in, please do. Feel like the more explanations I get with
different slants, the better. Thanks again. I love codecademy. It is the
first place I have found that truly gets beginners.


If we look at your posted code we can see that there are mixed spaces and tabs for indentation. To sort that out, remove all indents and replace with four spaces per block level.

Be sure your return line is on the first block level.


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