11/13 Making a Purchase returns non-accepted value



Can´t seem to get past this screen, probably because I do not understand well what I am doing. My code is as follows:

stock = {
“banana”: 6,
“apple”: 0,
“orange”: 32,
“pear”: 15

prices = {
“banana”: 4,
“apple”: 2,
“orange”: 1.5,
“pear”: 3

def compute_bill(food):
total= 0
for food in shopping_list:
total += prices[food]

return total

shopping_list = [“banana”, “orange”, “apple”]

When I run it, I get the following message:

compute_bill([‘apple’]) returned 7.5 instead of 2

Isn´t 7.5 the value I am supposed to get? 2 is the key for a specific string on the dictionary, but the exercise is asking to create a calculator on the total cost of a given list of items, thus an addition of their value. Or so I understood.

Any ideas on what I am doing wrong, and on how to move forward with this?



food is the parameter, so we cannot re-use the variable as an iterator. Change that to,

for item in food:


Thanks a lot!

Now it works, but I am not sure if I understand what was wrong with my original statement: was I limiting the work of the function to the shopping_list list, instead of making it open to any list of elements I would want to pass as parameter?

I understand I was using the same variable for two different functions, but the function seemed to be doing its work anyway (i.e. it was computing the correct cost of the items in the shopping_list ), so I guess the problem was it was not an open function, but rather a closed calculation.

Sorry for too many questions, I am still trying to wrap my head around all the new concepts here! :slight_smile:

Thanks again for the help!


Your function ignored the input given to it and read from a global list instead

a, b = 5, 6
def add(a, b):
    return a + b

That function should add the values given to it, it should not ignore them and always return 11 which is the sum of some globals

add(3, 2)  ->  5  # seems good
# but if you instead got...
add(3, 2)  ->  11  # this function is clearly broken


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