10.11 Why does this work?


#1

My Code (works):

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]
    
        #print total
        return total
    
    print compute_bill(shopping_list)

I'm completely lost right now :expressionless:
What causes this code to know what's in my shopping list when there is no reference to shopping_list?
Where does the argument food come in?
could someone give me a line by line explanation starting at ># Write your code below!


#2

@webwhiz99400 take a look at the code below with comments

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!

'''
    we are creating a method to compute bill
    we do this because we dont want to write that long
    implementation everytime we need to compute a bill
    
    think a method as a template used for some function
    the 'food' argument in the bracket is just saying that
    whenever we use this method we have to pass some argument
    in this case we call it food
'''

def compute_bill(food):
    #we create a total variable
    #this is where we are going to adding
    #the total so far each time
    #it is 0 now because w e have not yet
    #begun adding to our bill
    total = 0
    
    '''
        Now we loop through food remember food is just
        the list that we will pass to this method when we
        actually use it
    '''
    for item in food:
        '''
            Now here think of the 'stock' and 'prices'
            dictionaries a  some database(like the grocery store)
            that has all the items with prices and stock recorded
            
            As we go through the the list of item given (i.e food)
            we check that item against our database(dictionary)
            and add its price to the total
        
        '''
        total += prices[item]
    
    '''
        here we return total and not print it. 
        Whenever you return a value in a method
        it allows you to capture that value ans store it a variable
        and use it for whatever you want.
    
    '''
    return total
    
'''
    Now tha we have a a method(tempalate) that 
    will compute the bill of items taken, we will now
    use it to find the totobal bill of our shopping_list
    as we have defined above
    
    Remember whenever we use this method we have to pass
    a list argument and shopping_list is a list it will work
'''
print compute_bill(shopping_list)

#===========================================================================
#I can also have another list of items an compute the bills on those items
shopping_list_two = ["banana", "orange"]

#now i conmpute the bill but i have to pass shopping_list_two as argument
print compute_bill(shopping_list_two) # will give 5.5

#3

I am still confused why we use food as argument?

we did not define any variable as food and how does Python know what food refers to?


#4

Oh, that's easy.

It's the same reason we don't need to exactly have the same argument each time we call a function.

If you remember doing the hotel assignment a while back (in functions, maybe?), we can interchange the arguments. You've already specified what shopping_list was in the beginning of your code; when you called the compute_bill function, you specified shopping_list as your argument.

so, you can name the function compute_bill(balloons_and_clowns) for all you want;

as long as you specify the argument WHEN you call it, the function will work.


#5

Lets take a look at this code first:

print compute_bill(shopping_list)

shopping_list is defined as global variable, which is a list
and compute_bill(shopping_list) this function takes that list and inputs it into that function.

1    def compute_bill(food):
2        total = 0
3        for item in food:
4            total += prices[item]
5    
6        #print total
7        return total

When you declare variables inside a function(first variable declared is food, and then total), they are known as local variables meaning they only exist while this function is running. and since we called this function with shopping_list (a global variable), its copied into food (a local variable). so now food is a copy of shopping_list.
Now prices[item], on line 4, is not a local variable, when you use a variable that's not in the local scope, Python falls back to global scope, meaning it will search the global variables for prices.