Help with Compute_bill


#1



https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096


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


#2

here

value = prices[key]

key?


#3

That's not how to identify the value?


#4

Your iteration variable is item ?


#5

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


#6

I just did it, replacing key with item


#7

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?


#8

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


#9

How we loop through a dictionary..

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

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

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

#10

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.


#11

+reallyrosie
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.!!!
cheers!!!


#12

for item in food:
        value = prices[key]

Its not? (the code given above) ?


#13

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