11. Making a purchase


#1


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

When i run the code it gives an error message, "Oops, try again. compute_bill(['apple']) returned 0 instead of 2"

I suspect it is trying to run the function against the stock dictionary, but I'm not really sure why.


shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

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


#2

Hi,

Do you find something weird here? You haven't assigned a variable to the loop.

for item in food:
        total + (prices[item])

It should be something like this:
total = total + prices[item]

Happy coding :slight_smile:


#3

Hello

I would suggest that you look at lesson 8 - keeping track of produce.
Look at the 'for' section and your 'for' section.
Also look at the example in this lesson's calculation, you are missing something.

Hope that helps.


#5

yaa i did the same thing but it still doesnt work. its showing error. can someone please send the correct solution


#6

asking for answers/posting answers without explanation is not according to the guidelines, please make a new topic with your (formatted) code + error message so someone can help you.


#10

Try removing the parenthesis, and adding in the += instead.


#11

you're not adding anything to total. it runs apple and see
total=0
price[item]=apple[2]
0+2
but thats all. try
total = total + prices[item]


#12

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