Making a Purchase


#1

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(item)
    return total

The error is: Oops, try again. compute_bill(['apple']) resulted in a TypeError: 'dict' object is not callable
I guess I don't see it right, could you help me? ...


#2

here:

total += prices(item)

using parenteses makes it a function call, to retrieve value from dictionary using key, use square brackets


#3

Now it is telling me: Oops, try again. compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5 ... Why must it be 10.5? And I really do not see what I can change to receive 10.5


#4

can i see an updated version of your code?


#5

# Write your code below!
def compute_bill(food):
    total = 0 
    for item in food:
        total += prices[item] 
        return total
n =[1, 3, 5, 7]
print sum(n)

I just added the rest to see if it helps but it did not :smiley:


#6

this doesn't do anything:

n =[1, 3, 5, 7]
print sum(n)

a function ends the moment a return keyword is reached, so your function ends in the first run of the loop. You had it right, why change it?


#7

If I leave it, it still does not work

Write your code below!

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

error: Oops, try again. compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5
back to the beginning :frowning:


#8

it seems you only read 50% of my answer


#9

Okay, I think I will have to ask again later. I really do not see it at the moment but maybe with fresh eyes again.
Sorry ....


#10

currently, return total is inside the loop, this means your function ends in the first run/iteration of your loop, so only the price of the first product is added to total, you want to place return total outside the loop (like you had the beginning), so the price of all products is added to total


#11

I am back :smiley:
Now, I put it outside the loop, and it is telling me:
Oops, try again. Your code did not run to end. Check the console window for errors.
File "python", line 23
SyntaxError: 'return' outside function


#12

return should be inside the function, but outside the loop

you got the indent right here:

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

#13

Okay got it - Thank you!


#14

the code i posted in the previous reply was only to show how you should indent return..... It was the original code you started out with, which contained the parentheses which should have been square brackets, we covered this? Don't just copy paste, programming is about thinking, understanding.


#15

I know, I got it .... It just did not work as you said at the beginning... (I had to reset the code)


#16

I am also having an extremely hard time with this one. Have gone through all the exercises in this section again to review and have spent the better part of a day on trying to faff this out. Where can I see the solution to this conundrum?

Thanks in advance,


#17

My explanation was for mel_94, if this explanation doesn't suit you, go here, press the make new topic button, fill in the template so we have all the details needed to help you


#18

Ok so in having the same problem and looking through the boards of the same question for about a half hour. I've noticed that we all are overlooking formatting. The way you had it formatted when you first posted is correct the only thing that needs changing is " total += prices(item) to this total += prices[item]"


#19

Thanks, now that is useful response!


#20

Thank you man! just glad I could help.