None Save & Submit Code Reset Code Oops, try again. compute_bill(['apple']) returned 0 instead of 2


#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 x in food:
if stock[x] > 0:
total += prices[x]
stock[x] -= 1
return total
None

None

Save & Submit Code
Reset Code
Oops, try again. compute_bill(['apple']) returned 0 instead of 2


#2

@bc136694,
Do not manipulate the stock dictionary by substraction.....
Just use the Instructions

  1. Define a function compute_bill that takes one argument food as input.
  2. In the function, create a variable total with an initial value of zero.
  3. For each item in the food list, add the price of that item to total.
  4. Finally, return the total.

Ignore whether or not the item you're billing for is in stock.

Note that your function should work for any food list.


#3

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

print(compute_bill(['banana']))

I get a 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}

Can someone help me out


#4

stock is decremented for banana:
4
None