Stocking Out


#1



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

Oops, try again. calling compute_bill with a list containing 2 apples, 2 pears, 3 oranges and 7 bananas resulted in 22.5 instead of the correct 34.5


Help pls I just don't know what i am doing wrong!!!!!.any help would be great, have looked at other people's code and the seem to be the same.


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:
        if stock[item] > 0:
            total += prices[item]
            stock[item] -= 2
        
    return total


#2

You have it setting stock to 2 less instead of 1 less. You should replace the 2 with a 1. Like this:

def compute_bill(food):
total = 0
for item in food:
if stock[item] > 0:
total += prices[item]
stock[item] -= 1
# Right here ^

return total

#3

A post was split to a new topic: It doesn't work


#4

@amina.d You're calling the function again with prices as an argument. Right in the error message it tells to NOT call compute_bill since it changes the stock.
Your code should end right when returning the total.

@edward_green Can you please explain what stock[item] -= 1
I'm still struggling to grasp it.


#5

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