My code is working but site give error, any advise


#1

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

compute_bill(shopping_list)
print stock

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}


#2

In your code

compute_bill(shopping_list)

In your error message

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}

#3

this is my code
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 += prices[item]
stock[item] -= 1
return total

compute_bill(shopping_list)
print stock

and this is my error
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}


#4

Read the error message!


#5

the error tell me to remove the stock[item]-=1
if i remove this line then the error is

Oops, try again. calling compute_bill with a list containing 1 apple, 3 pears, 3 oranges and 8 bananas resulted in 45.5 instead of the correct 37.5

but i have to follow the 2nd instruction that is

If the item is in stock and after you add the price to the total, subtract one from the item's stock count.


#6

I am seeing exactly the errors as abishek. When I print the modified stock dictionary I get the correct

apple 0
pear 15
orange 31
banana 5

Which is what I expect to see after I decrement the available stock items on my shopping list.


#7

This is silly. To get to the next module, do not call compute_bill. This will get you unblocked. They want you to write the function, but not call it.


#8

Thanks Pablo, that was indeed the answer I was looking for!