Can't Print Total in 12/13 or 13/13


#1

I know it's not necessary but I'd like to print the total so I can see how everything added up. However, I'm not getting anything printed. Any advice?

 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:
        if stock[item] > 0:
            total += prices[item]
            stock[item] -= 1

    return total
    print total

#2

try

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:
        if stock[item] > 0:
            total += prices[item]
            print "total is now %s"%total
            stock[item] -= 1

    return total
    print total

then finally call it like this

print compute_bill(shopping_list)

#3

Thanks rydan! I had the same question.

This worked for me, and I understood how to code everything after setting up the for loop, but I never quite figured out what food was doing in the code.

Is it just a generic argument, and if so, how does the function know to look at the stock and prices dictionaries?


#4

@sardawson

Well, in this case

def compute_bill(food):

Food is a function parameter also know as a function variable or an argument.

As you should know by now a variable can be anything we decide to make it, in the case of a function each parameter is assigned the value in the order it is listed unless you name them and describe them by name,

EXAMPLE:

def function_call(p_one, p_two, p_three):
    print(p_one, p_two, p_three)

Now we can call this function two primary ways, one we can just throw three different values in our call and it will function correctly but as I said before the values will be assigned in the order they are declared.

Example

function_call(3, 2, 1)
# OUTPUT: 3 2 1

We can also call it like the following

function_call(p_three=3, p_one=1, p_two=2)
# OUTPUT: 1 2 3

Now as you can see the order does not matter in this case because when we declared the function the order is predetermined. So when we we use the name of the parameter in the function call that is called a key word argument.

We can use any combination of the two above when calling a function but the order must ALWAYS be unnamed arguments followed by key words

EXAMPLE:

function_call(1, 2, p_three=3)

If you mess this up it will simply throw an error and tell you where you did so.

Needless to say though that variables in a function allow us to do some magical things with programming.

EDIT:
@dobrien323

Before I forget to mention this,

return something

This inherently breaks out of a function. Once you call return there is no going back in a function. You have to do all work you need done before you call the return.