11: Making a Purchase Question


Hi everyone, I had a question regarding the for loop in this challenge.

If I have this 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 item in food:
total += prices[key]
return total

I error that I'm returning 8 and not 10.5.

However, if I change the for loop to

for key in food, I get the expected result. Can someone explain this one to me?


The iteration variable's name makes no difference. You changed something else as well.
You use the name key in your loop, but haven't defined it. By renaming your iteration variable to key, suddenly you have defined it.


Your code is resulting in an error because code academy wants this function to be able to take any dictionary list and output the total so do not use the dictionary lists above as part of the function (do not use prices[key])

I am also getting an error although I have verified that the code preforms the desired result and it works in every case. My code looks slightly different

def compute_bill(food):
total = 0
for item in food:
total = total + food[item]
return total

thanks in advance

11, reaching the end

def compute_bill(food):
for key in food:
total+= prices[key]
return total

I had quite a trouble with this as well. This is the working code:
you use the price dictionary and you add the price of the item to total.


If I'm not mistaken, this is what you say is the working code:

# Write your code below!
def compute_bill(food):
    total = 0
    for key in food:
        total += prices[key]
        return total

it's not working for me. It gives me this

Oops, try again. compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5

I need help too though :smiley:


It's pretty particular about the spacing on the return command, try getting rid of the space before that command and it should work.


Hi, you should put the return outside the loop.


I have to apologise, because, although the
def compute_bill(food):
function works with
total += prices[item]
in the for loop, I am still confused as to how to make the function generic without using any identifiers from the preceding programme. Is it possible to produce a definitive solution to the problem, given that there may well be alternatives to a solution?



i think the answer might be in the example they provided at the side, I'm still getting an error but the output seems correct, the flexibility for the code to accept any list/dictionary is the way the function is called at the end ... i.e "print compute_bill(stock)" or "print compute_bill(prices)" see below:-

def compute_bill(food):
total = 0
for item in food:
total += food[item]
return total

print compute_bill(stock)

im getting the right answer but also an error :-

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

anyone know what this means ?



ok, an update, i misunderstood what the instructions were ultimately asking us to do.. "any food list" means the shopping list at the top of the script ! so please see below :- (its probably not the most efficient way of checking whats in the shopping list but it works , if anyone can refine please let me know)

def compute_bill(food):
    total = 0
    for item in food:
        if item == "banana" :
            print "banana"
            total += prices[item]
        elif item == "apple":
            print "apple"
            total += prices[item]
        elif item == "orange":
            print "orange"
            total += prices[item]
        elif item == "pear":
            print "pear"
            total += prices[item]
    return total
print compute_bill(shopping_list)


hi. The error occurs in the indentation. the last line "return total" should be at the same level as "for item in food" and not indented.


I think your code works only for single item as it if-else-if loop. If banana is the item it adds banana'svalue to total and gets exit. It won't check if there are anyother items in the list or not.


Getting the same problem! :frowning:


i know this works but i just cant understand why??? did you get it?


I think almost.

First with def compute_bill(food) we create the Function compute_bill with an argument (variable) called food.
In the Loop we tell that for our function we take the zero and add the price of each part of a list.
at the end we return total to get the sum.
Now we print the returnd answer of the function compute_bill() running the function with our choosen list (shopping_list)

I think food is just a variable and every existing list can be put in running the function and printing the returning sum.

Hope this is understandable.


oooh. i got it. thank you!!


ah that makes sense :slightly_smiling: cheers mirco_schmidt :slightly_smiling:


you have to press backspace right before the word return


Write your code below!

def compute_bill(food):

total = 0

for x in food:

    total += food[x]
return total

print compute_bill(prices)

When I run this, I get the correct answer (10.5) but I am still getting an error.


This code worked for me: