# Why does this work? (for item in food)

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
enquiry

<What do you expect to happen instead?>
why does for item in food work? i thought that for item in references a list or dictionary, yet the list and dictionaryâ€™s are called prices and stock and shopping_list.
i get that iâ€™ve defined compute_bill (food) , but how does it know that iâ€™m talking about stock and prices on the loop? , sorry if iâ€™m not making myself clear iâ€™m struggling to understand the logic of how the code knows that looping through food actually loops through stock and prices

```python

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

``<do not remove the three backticks above>``

#2

Look here:

In our function, we have one parameter, `food`. Itâ€™s just a placeholder when calling a function and adding an argument. So if I called my function anywhere outside of it after defining it like this:

``````print compute_bill(prices)
``````

`prices` is the argument, it takes place of all the `food` placeholders in your function. It doesnâ€™t replace them physically, but in the background, Python knows that `prices` is `food`, and anything that happens to the `food` parameter will happen to `prices` (or whatever argument you choose). Basically, the argument will take place of `food`. Do you get what I mean?

#3

so definitions in the same script always know any lists or dictionaryâ€™s present ? and the temporary argument of food only exists within the following indented text?
thanks for the help

#4

In all of your code, functions, all of that stuff, so yep. A thing to remember though:

If I defined my function after my dictionary, then called anywhere before the line where it was defined, I would get a `NameError`. So, donâ€™t call your function in a place before it was defined in your line of code.

Yes, if I called `food` outside of my function, I would get an error (`NameError`) saying `food is not defined`.

Youâ€™re welcome!

#5

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