12. Stocking Out


#1



10. A Day at the Supermarket

Oops, try again. stock doesn't look quite right! Make sure to not call compute_bill since it changes the stock! It should contain: {'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}


According to instructions: If the item is in stock and after adding the price to the total, subtract one from the item's stock count.


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

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4.0,
    "apple": 2.0,
    "orange": 1.5,
    "pear": 3.0
}

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

print compute_bill(shopping_list)


#2

@systemcoder43338 ,

The message is asking you not to execute this call to the compute_bill function ...

print compute_bill(shopping_list)

You can delete or comment out that line and execute it in the next exercise instead.


#3

Thanks. You are right!!


#4

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):

count = 0
for item in food:
     if stock[item] > 0 :
        count=count+prices[item]
        print count
        stock[item]= stock[item]-1
        print stock[item]

        return count

a = ["banana", "orange", "apple"]
print a

x = compute_bill(a)

What's wrong in this code!!!!!!.......


#5

Hi @coder_chita ,

A function terminates execution and returns a value immediately when a return statement is executed. In consideration of that, should this statement be indented to the degree that it is now, which makes it part of the for loop and the if block? ...

return count

Do you need to have a print statement within your compute_bill function, when you can instead print the result of calling the function?

When you submit your code, Codecademy might ask you to postpone calling your compute_bill function until the final exercise.


#6

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