Stuck in "A Day at the Supermarket - Making a Purchase" - please help!


#1



Given 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 comput_bill(food):
total = 0
for key in food:
total += food[key]
return total


error message:
Oops, try again. compute_bill(['apple']) resulted in a TypeError: list indices must be integers, not str

pass that 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 comput_bill(food):
    total = 0
    for key in food:
        total += food[key]
    return total


#2

the function you should be called compute_bill, the e is missing in your case. Also, this line:

total += food[key]

why access the food list with key? You want the price of an item, why not use the key to access the price of the prices dictionary?


#3

sorry, compute_bill is already corrected.

I thought food[key] is access the price of prices dictionary. The exercise I had right before that is bellow which is similar to this exercise:
prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}
total = 0
for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]
total = total + prices[key]*stock[key]

print total

i.e. prices[key] and stock[key] are accessing the prices of prices and stocks


#4

also if I added below code:
print compute_bill(prices)

the console out put on screen is:
1.5
3
4
2
10.5
None

Does that mean total is correct in prices? each number is the price in prices


#5

this is confusing, you are now using two pieces of code combined, i can't make sense of it


#6

the code above comment # Write your code below!
are given from the exercise, I only wrote the code under that comment according to the instruction.
please tell me what is wrong.


#7

i told you want is wrong, you need prices[key] to get the price of the item


#8

ok, this is the instruction:

Instructions

  1. Define a function compute_bill that takes one argument food as input.
  2. In the function, create a variable total with an initial value of zero.
  3. For each item in the food list, add the price of that item to total.
  4. 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.

Here is my code:
def compute_bill(food):
total = 0
for key in food:
total += food[key]
return total

Could you write the correct code please?


#9

@stetim94 is right. You need the line "total += prices[key]". I'm thinking the reason why your original code worked for the problem before, and stop me if I'm wrong, is that the problem before gave you two dictionaries and the current problem gives you two dictionaries and one list.

If you run compute_bill(shopping_list), you are loading a list and not a dictionary in your function and your for loop. So, total += shopping_list[key]. You don't have any integers or floats there. Just strings. This is why you need "total += prices[key]". No matter what variable you throw at your function it will always look at your price dictionary.

The hint that codecademy gave was "Note that your function should work for any food list."

P.S. I'm learning too, so be gentle with the feedback. :slight_smile:


#10

ok, got it. it works. thanks!