Help


#1

in the following code I need some clarifications please

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:
    if stock[item] > 0:#
      total += prices[item]
      stock[item] -= 1
  return total

because I call stock [item] directly, which means that expression item are the values ​​within
Same in prices[item]

another question - = or + = what do they mean?


#2

call is a confusing term, you access a dictionary by key to get its value. What is the question about this?

they are short hands:

x = x + 5
x += 5

both those lines of code are the same, one just uses a shorthand


#3

my question is about (for) because if the order is:

def compute_bill (food):
for item in food:

it is taken for example:
stock [item]
prices [item]

if item is in food, because it is taken in stock and prices


#4
def compute_bill(food):
  total = 0
  for item in food:
    if stock[item] > 0:#
      total += prices[item]
      stock[item] -= 1
  return total

compute_bill(shopping_list)

lets at a function call, so you know the value of food. So item will contain values from food list. We can even now insert a print statement to see this:

  for item in food:
     print item

then we use item as key for both dictionaries

remove the function call after you understand, otherwise you can’t pass the exercise


#5

but in this prices [item] which means there?

Who takes the key-value of the dictionary of prices?


#6

look:

def compute_bill(food):
  total = 0
  for item in food:
    print item
    if stock[item] > 0:#
      total += prices[item]
      stock[item] -= 1
  return total

compute_bill(shopping_list)

you will see that item will be the value of products in shopping_list.

for item in food will loop over all items in food, each iteration of the loop assign the current value to item


#7

my doubt is that as an article was taken in order to food, because you can relate article with prices prices and action if in the statement of for food

for example:
stock [item] = stock dictionary values


#8

Sorry, but i do not understand you.


#9

in the code where it was made so that the total of each meal was added up?


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:
    if stock[item] > 0:#
      total += prices[item]
      stock[item] -= 1
  return total

food= ["banana", "banana","orange"]
print compute_bill(food)
print stock

#10

here:

total += prices[item]

you add the price of item (which you name meal) is added to total


#11

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