FAQ: Learn Python: A Day at the Supermarket - Stocking Out

Thanks, your input really helps. You are well appreciated.

Still a noob and learning. Please excuse my ignorance:

How does the loop know that it should pick items mentioned above the loop?

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 i in food:
total = total + prices[i]
return total

The function allows us to compute the bill for each customer without having repetitive code.

so now we can call the function, each call represent a customer buying items from your store:

compute_bill(['apple'])

so…I have created a dict stock and prices
then defined a function compute_bill with food.
this function has nothing to do with the words in the dictionary.
then i ran a loop for i in food. - again food is not anywhere in the dict.

how did python know I am refering to the words in the dictionary ?

yes, this is your store. The products present in your store and the price of the products

this is your checkout/Cashier.

this is not true. When we call the function:

compute_bill(['apple'])

food function parameter will now receive ['apple'] as argument/value.

so now the function will loop over the products in food, here:

for i in food:

which is apple, we can see this if we add an additional print statement:

for i in food:
    print(i)

so now we look up apple in our prices dictionary here:

total = total + prices[i]

and add the price of apple to the total bill of the customer.

you don’t do anything with stock yet, that is the next exercise. To prevent customers from buying things you don’t have in stock

:cry: still dont understand:

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 i in food:
total = total + prices[i]
return total

I have a list, then two dictionaries.

I have defined compute_bill(food)

how did my “for i in food” statement, know that it has to pick something up from the list or dict above?

i have not defined “food” anywhere. How does it know?

In this list :cry:

my_list = [“mon”,“tue”,“wed”,“thu”,“fri”]
for i in my_list:
print (i + “day”)

the "for i " statement checks “my_list”
my_list is defined

in the previous code, food is not defined.

hence my confusion.

food is a parameter. Parameters act as placeholders until they get a value from argument at function call, like I showed you:

compute_bill(['apple'])

You can just copy paste this call and use it in your code

otherwise, I would recommend you to revisit the lessons on functions, parameters and arguments. Other consult an external resource

Side question: I am doing this lesson and I have currently encountered a blind spot. I do not understand how ( food and item ) got a identity and how python equates those words to be a reference to stock and prices. Can someone please explain this? Yeah, I am being a bit dense today.

food is the function parameter, which me we can provide the value as argument at function call, for example:

compute_bill(['banana', 'banana'])

so the argument (['banana', 'banana']) gets passed to the parameter (food)

then we use a for loop to loop over the food items, giving us the values from the list. Given our list has the same item twice, the iterator (item) will hold banana twice

we then use this key (item variable holding string value banana) to look-up values from the dictionaries

I understand to a degree. My mind operates in the simplest terms so my task now is to equate these concepts in a way that makes sense to me.

Thank you so much for your help

You could use a tool like this one:

Python Tutor - Visualize Python, Java, JavaScript, C, C++, Ruby code execution

to step through your code and break it down? That might help depending on what type of learner you are