Discussion about for and in


#1



A Day at the Supermarket - Making a purchase

I got my code to work but I don't quite understand why it does.

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

In line 3 I call "for items in food". What does food represent because none of my lists or dictionaries are called that. I'm confused by how you can use food and it makes computations.


Replace this line with your code.


#2

We are given a grocery list in the exercise setup. We write the funciton then pass that list as the argument in our function call.

def compute_bill(food):
    # function block code

compute_bill(grocery_list)

In this instance, food is the locally defined reference to the grocery_list object. When we are iterating over food we are actually iterating over gocery_list.