Making A Purchase


#1



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


Hi

For this exercise I used the below code and it works but I'm not entirely sure why it works. Everything makes sense to me apart from the use of 'food' in the function. How does Python know that when I type 'food' it is referring to 'shopping_list'?

Thanks


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


#2

it doesn't, until you call the function. A parameter is just a placeholder until you call the function and supply an argument


#3

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 the the program could care less. It's just a placeholder for the 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(shopping_list)

You would essentially be telling the program to run compute_bill(food) except now, food = shopping_list. 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.


#4

Ok, thanks aquaphoenix. I think I understand. So the code which I have is successful for Codecademy purposes but it isn't actually a working code per se. It would only work when I put in a parameter which has a value. Is that correct?


#5

work is a vague term here. Your function is syntax wise correct. (otherwise you would get syntax errors)

It just that in the script your wrote, you never call the function. Your script won't execute the function unless called

When you call the function (in which you supply an argument), the function will copy the argument into the function parameter, and run


#7

What I meant by:

Is if you did this:

shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

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

compute_bill(shopping_list)

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

total = 0
for item in shopping_list:
    total += prices[item]
return total

#8

Thanks, that's cleared it up for me.


#9