Why does this work?


Hi, I have the code working, however I don't know why it is working. My question is, how does the newly defined function know to reference the 'shopping_list' list? I just don't know how it traces back.

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


Well, your function takes an argument:

def compute_bill(food):

your argument is food, which means if you call the function:

shopping_list = ["apple","banana","orange"]
print compute_bill(shopping_list)

if this is still to confusing, you can parse the list as function argument:

print compute_bill(["apple","banana","orange"])

now your food variable (the argument of your function) is the list with all the things you need, the beauty of functions. With some simple code we see this work:

def showing_list(argument):
    # we pass the list into argument
    print argument
    # and to see each item:
    for i in argument:
        print i

for next time, use one of the two following options to make your code/indent visible:

select your code and press ctrl + shift + c (or cmd + shift + c if you use a mac)

if this instructions are unclear, you can also insert 3 backticks before and after your code, like so:


the backtick is located above the tab key on your keyboard


Thank you very much for the reply! :grinning:


1.Interpreter will jump over your function
2.interpreter will find your variable "shopping_list"
3. will create an object list type in memory, ["apple","banana","orange"], then will bind this object to symbol name "shopping_list", actually the memory address where object reside , you may name this bind as pointer
4. Next interpreter finds "compute_bill(shopping_list)", this tells to interpreter to find and execute your function
5. in your function interpreter finds parameter "food", actually is a local variable
6. interpreter will bind object already created in step 3 to local variable "food" , now your object is referenced by two variable, first one "shopping_list" as global variable and second one "food" as local variable, both points to ["apple","banana","orange"]
7.next interpreter finds "total=0", will create another object integer type which store value '0' and bind this object to symbol "total"
8. for "for" loop interpreter will create another local variable "item" then interpreter bind each element of your object that has symbol name "food"-> ["apple","banana","orange"] to symbol "item"
9, for first iteration your local variable "item" will point to first element in list, "apple" -> item='apple'
I suppose that "prices" is a dictionary that identify price by name eg. "apple": 10
10. totat = total + price[apple] -> total = 0 + 10, this mean that "total" bind to object 10, total = 10 for first iteration
11 because in "food" still remain elements for will start again
12. "item" will bind to second element in "food", "banana". Actually will remove reference from object "apple" and relocate this reference to object "banana" and continue as in steps 9 -12 until all element in "food" will be referenced and add to "total" prices
13. Interpreter will finds "return total" and pass value to the caller, actually the reference to total
14. then table for local symbols will be destroyed, this means that reference from "food" to abject ["apple","banana","orange"] will be removed and only global variable "shopping_list" will point to it.