Issue with <stocking out> part.12


#1

I have no idea about the mistake I made in this practice.
Could anyone help me have a look in it?
thanks!

I got error like "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}"

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:
        if stock[item] > 0:
            total = total + prices[item]
            stock[item]=stock[item]-1    
    return total
print compute_bill(shopping_list)


#2

Remove the if statement,

and this decrementation,


#3

if you remove that statement, you don't check for available stock
if you remove the "decrementation", the stock doesn't leave the shelf...

so, we'd be exactly back to where we started with the previous code...


#4

the answer is actually in the error message:

Make sure to not call compute_bill

yet you call compute bill, which will change the stock.


#5

Hello @stetim94.
Could you explain more about where did i call compute_bill?

The first and only time i called compute_bill in my code is the last line while printing it out. Then how do it affect the stock?

Thanks for your assistance and time :slight_smile:


#6

this is the function call:

print compute_bill(shopping_list)

well, calling the function causes the function to execute, reducing the stock:

stock[item]=stock[item]-1

before the exercise code can be validated, the error message says so as well


#7

I didn't call any function and mine code is showing not giving correct figure 7


#8

if you have a question, go here, make a new topic, with this little info its really difficult to help you


#9

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