Print the output of compute_bill


#1

shopping_list = ["banana", "orange", "apple"]

stock = {
-- "banana": 6,
-- "apple": 0,
-- "orange": 32,
-- "pear": 15
}

prices = {
-- "banana": 4,
-- "apple": 2,
-- "orange": 1.5,
--"pear": 3
}

for key in prices:
-- print key
--print "price: %s" % prices[key]
--print "stock: %s" % stock[key]
--total=0
--total+=prices[key]*stock[key]
--print total

def compute_bill(shopping_list):
--total=0
--for key in shopping_list:
--if stock[key]>0:
---- total=prices[key]+total
--stock[key]=stock[key]-1
--return total

call compute_bill(shopping__list) #?? I'm not sure how I'm supposed to call it.

My teacher wants me to print the output function of compute_bill witth shopping_list as the argument.


#2

We can write this line to include a generic parameter variable, such as,

def compute_bill(groceries):
    total = 0
    for item in groceries:
        if stock[item] > 0:
            total += prices[item]
            stock[item] -= 1
    return total

The call to the function with shopping_list as argument...

compute_bill(shopping_list)

#3

So all I really need to do is type
"print compute_bill(shopping_list)"?


#4

That's it, just not in quotes. As we have seen, it matters not what we call the parameter variable, and even if we use the name in both argument and parameter, they are two separate variables, one local, the other global. A good practice would be to not use the same name so your code is more easily readable.


#5

Oh ok. Thank you! I did notice how you can use any variable.

I'm actually stuck on a different project. It seems really simple but I've been trying to get it for a few days now..