Where is food defined in Making a Purchase?



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


Hi food is just the parameter of the function comput_bill


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?


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.


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

print compute_bill(shopping_list)


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.


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