11: Making a Purchase Question


#1

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?


#2

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.


#3

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
#4

def compute_bill(food):
total=0
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.


#6

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

right?
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:


#7

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


#8

Hi, you should put the return outside the loop.


#9

Hello,
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?

Thanks.


#10

hi,
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 ?

cheers


#11

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)

#12

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.


#14

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.


#15

Getting the same problem! :frowning:


#18

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


#19

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.


#20

oooh. i got it. thank you!!


#21

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


#22

you have to press backspace right before the word return


#26

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.


#27

This code worked for me: