Where is food defined in Making a Purchase?


#1



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

The code I wrote worked correctly and I passed the lesson, but I don't understand how it works. Food is never defined before the function, so how does it know to use the items in shopping_list in the for loop?

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

Hi food is just the parameter of the function comput_bill


#3

Okay, but still, how does it know to use the items from shopping_list instead of from any other list (hypothetically) since there's nothing in the code to connect the two?


#4

It doesn't. It's a parameter, its value is provided by the caller.
There's never any "knowing" going on, you're lining up events to obtain some kind of result, it's all up to you to make it fit. All of it.


#5

YOu just need to call the function compute_bill with as argument the shopping_list list like that

print compute_bill(shopping_list)

#6

Oh okay! Reading that made it click all of a sudden, thanks. I guess I was just confused because the lesson only had me define the function, not plug it in after.


#7

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