Help with Compute_bill


I'm getting the wrong value - 8 instead of 10.5

I can't figure out what I'm doing wrong here. Please help

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:
        value = prices[key]
        total = total + value
    return total



value = prices[key]



That's not how to identify the value?


Your iteration variable is item ?


I've tried everything. I'm honestly not understanding the logic anymore. I wanted to access the value in the prices dictionary.


I just did it, replacing key with item


I'm not sure I understand it. How did it take the value and not the word?

So I could have skipped the value part and done simple total = total + prices[item], and it would have understood it was to grab the value, rather than the word?


Refresh the page and try to submit again!(it works for me)


How we loop through a dictionary..

x = {"a": 1, "b" : 2}

for i in x:
    print x[i] 
# it will print 

Have I used any key to access values?
No, I have used a general variable i .(It could be named like any general variable for eg. m, n,k,key, etc)

but suppose I wanted to add value of "a" and "b" then I'd have done this..

x = {"a": 1, "b" : 2}

total = 0
for i in x:
    total = total + x[i]

print total # It'll print 3


I think I'm going to have to go over key and loops again. Thank you for your exercise, I think that's the problem I'm having.


your code is 100% correct, and it should work.. if its not then its a bug on the codeacademy website.. cos I tried the same and it kept giving me an error. the best way to check is .. write the same script on python idle on your computer.
I just reset the code on the page or reloaded the page and the same code worked.!!!


for item in food:
        value = prices[key]

Its not? (the code given above) ?


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