How does python know that food is my shopping list?


#1

Hi, could someone explain me why i don´t need to relate "food" to my "shopping_list" and in the code? My code works but i just don´t get why :smile: Thanks

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


#2

Hi @ykwtd ,

In your program, food does not yet refer to your shopping_list. To make that connection, you need to call the compute_bill function, passing it shopping_list as a parameter. But Codecademy wants you to wait until Exercise 13: Let's Check Out! to do that. In that exercise, you may call the function and print the result.


#3

Ahhh, thanks a lot for the info. Was really confused but now it´s clear. Thanks


#4

Thanks for your explain, I'm now confused about the relationship between food and prices? Although the code works, I still don't understand the function of prices[key], it should be food[key] here, I think.


#5

Suppose you pass "banana" as the argument to the function compute_bill
like compute_bill("banana")

now according to the code
for key("banana") in food
total += prices[key] would be total += prices["banana"]
prices"key" ie prices "banana"

it will look for "banana" in prices dictionary and will compute the sum

Hope that helps


#6

you have not called comput_bill(food) yet.