# Quick Question Regarding How This Code Exactly Works

#1

I have recently begun learning Python and this is my first time attempting to do anything related to coding at all so bear with me.

The code in question is as follows:

shopping_list = [“banana”, “orange”, “apple”]

stock = {
“banana”: 6,
“apple”: 0,
“orange”: 32,
“pear”: 15
}

prices = {
“banana”: 4,
“apple”: 2,
“orange”: 1.5,
“pear”: 3
}

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

I know that food is the argument for the function but I don’t understand how the code is read by the computer. I guess I am trying to understand what food and item are associated with. total and prices are defined but food and item are not? I can kind of understand that anything in the brackets after prices (the name of one of the dictionaries) is associated with the key of that dictionary (?). Maybe I am wrong and have no idea what is happening but any insight on whats going on here would be great.

#2

function parameters (`food` in your case) are placeholders, they get there value from argument at function call. Given functions only executed when called, this is possible

so at the moment `food` has no value, but we could pass `shopping_list` as argument at function call to give the parameter a value

#3

How does the code know to call on shopping_list to give food the value?

#4

it doesn’t, that is up to you. You can pass `shopping_list` as argument to function call

of course, you could pass any other list with products as argument. `shopping_list` was just an example (given you already had it in your code, i used it)

#5

I might be a little confused because when I ran the code, code academy said it worked. But I never explicitly told it to call on shopping_list. I guess I don’t see how the function of compute_bill is associated at all with the dictionaries and list above other than the one time total is recalculated using the prices dictionary in the “total = total + prices[item]”. So is that part of the function me telling it to call on the dictionary prices ?

#6
``````m = 5
n = 13
total = x+y

``````

Here is another example where the code works and adds 5 and 13 and prints 18 but why does it associate x and y with m and n. They are two different variable so how is the computer reading this?

#7

you don’t call shopping_list, you can call the function if you want it to execute

it does, the arguments `m` and `n` are passed at function call:

``````print add_function(m, n)
``````

the arguments will pass there value to the parameter(s)

#8

Oh okay I get it now, thanks for the help!

#9

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