11/13 Making a purchase - How do I read this?



I don’t think I need any adjustments to my code, because the program “accepts” it.

My questions are: How do I read the function and how does Python read my function?
My first assumption for the loop in the function was:

 for x in shopping_list:

rather than

 for x in food: 

How does Python/the function know that I want to call the items in shopping_list?
The loop doesn’t contain shopping_list…

If I am reading the code and try to “think” as python than I would say: (with the loop as: for x in_shoppinglist)

variable total is 0
Everything from the list shopping_list gets a value x
I should start with the variable total as 0 and adding the prices from the list x

But if I use the correct code:
I would say python doesn’t know I need the items from the list shopping_list.

What if i got a huge code, how does python know I want the items from the shopping_list?

I hope you guys understand my questions.
Thanks in advance!

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


food is the local parameter, a placeholder that takes the shopping_list object from the caller…


We pass the list to the function where it is given a local name.


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