12.Stocking Out


#1



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


Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7


Check if theres a item in stock and after that subtract 1 from the amount of the item inside the stock


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] = stock[item] - 1
        
    return total


#2

Your code is completely correct! :slight_smile:

Please refresh the browser and make sure that you are not executing the function (for example print compute_bill(shopping_list)).


#3

It was a small typo. i corrected it now lol, but anyways ty for the help :slight_smile:


#4

Write your code below!

def compute_bill(food):
total = 0

for item in food:
    if (stock[item] > 0):
        total += prices[item]
        stock[item] = stock[item] - 1
    else:
return total

I did all the same except "else:" and it displays like below.

return total
^
IndentationError: expected an indented block
[Finished in 0.035s]

I don't get it why. Could you explain it why?
Thank you in advance.


#5

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

Hope this is helpful! Please dont forget to use the right indentation it is important.


#6

Because you need to return the total if(condtion) is true not in the contrary case and your "return total" need to be in else statement in your code.


#7

This is like your code should look:
for key in food:
if(stock[key] > 0):
total += prices[key]
stock[key]= stock[key]-1
return total

In your case item=key


#8


#9

calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 9 instead of the correct 7
def compute_bill(food):
total = 0
for item in food:
total += prices[item]
return total
if (stock[item] > 0):
total += prices[item]
stock[item] = stock[item]- 1
return total
Help please.


#10

my code worked without using "else""


#12

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