Question regarding how the code works


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096


Hello,

I would like to know how it is possible for the code to know what food is. Is there something else going on in the background that makes it know it? Like we never tell it that food is banana, apple, orange and pear. So how does it know?


def compute_bill(food):
    total = 0
    for n in food:
        total += prices[n]
    return total


#2

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.


#3

Thank you for the detailed answer, you made it very clear.

However, nowhere in this page of code am I determining what food is:

Yet when I click Save & Submit it tells me the code is correct. It would all make much more sense if there was a line of code like the one you mention compute_bill(["banana", "apple"])


#4

You haven't called the function yet, you have merely defined it. As I previously said,

The code within a function only runs if you call it. Without a function call, the program just ignores the function definition. The reason why you pass the exercise is because Codecademy's SCT has confirmed that all the requirements have been met and that your function would work as intended if you were to call it.

"If a tree falls in the forest and you're not there to hear it, it still makes a sound."

"If a defined function works and you don't call it, it still works."


#5

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