While My code is correct, I dont know why it is


#1



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


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!


What I wonder is how the loop knows to iterate over [item] in our list (shopping_list)


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


#2

in a way, it doesn't. food (the function parameter) is a placeholder, until you call the function and supply an argument (shopping_list), the parameter and argument are linked


#3

I still dont quite get it. what do you mean that the paremeter and argument are linked, arent those two words just synonyms for a functions input?

Thank you so much for the fast response tho. Amazing community here.


#4

linked isn't really the technical term, the value of the argument gets copied into the function parameter. So, a function parameter (food in your case) is a placeholder till it gets value from the argument you supply at function call:

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

# function call with argument shopping_list
# value of shopping_list gets copied into function parameter
print compute_bill(shopping_list)

#5

Oooh right, yeah totally get it now. The function will work for any list, forgot that. And until we call that list with shopping_list as an argument, nothing really happens..

Thank you!


#6

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