Help Me:A Day at the Supermarket Making a Purchase


#1

Define a function compute_bill that takes one argument food as input.

In the function, create a variable total with an initial value of zero.

For each item in the food list, add the price of that item to total.

Finally, return the total.

Ignore whether or not the item you’re billing for is in stock.Note that your function should work for any food list.

i don’t know where i wrong?
Please help me. this my code

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+= key
  return total
print (compute_bill(shopping_list))

Thanksyou.


#2

lets add a print statement:

def compute_bill (food):
  total = 0
  for key in food:
    print key
    total+= key
  return total
print (compute_bill(shopping_list))

we can see key are the products (banana, orange and so on)

but we need to add the price to total, maybe we can use our prices dictionary somehow?


#3

compute_bill([‘apple’]) resulted in a TypeError: list indices must be integers, not str


#4

i know what the error message is, not important. Did you add the print statement i showed you? Then you see key is a product of type string, string + integer isn’t good. You need to get the price of item and add it total

If you need more help, post an updated version of your code


#5

how can i do add the price of that item to total.? please help me a line code


#6

we have our prices dictionary, and key (which contains our items/products) so we can use key as key to get the price value from dictionary

Ideally, you should make this thinking step yourself.


#7

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