11/13 Making a Purchase- Why does this work?


#1

Why does this code work? Here is the code that I used for this exercise:



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


My question is why is food used at the end instead of the name of the list shopping_list, especially in the for loop? How does the computer know what ‘food’ is?


#2

it doesn’t, the function parameter food is a placeholder until you call the function:

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

print compute_bill(shopping_list)

then the argument at function call gets passed into the function parameter

this is how function parameter and arguments work


#3

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