Issue with Stocking Out (this one)


I don't know what is wrong and the error message shows:

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

My code reads:

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

Thanks for the help :slight_smile:


A function ends when the return keyword is reached. Do you know where to put it? :wink:
(You want it to return the whole total. All of it basically.)


I don't get it... sorry :confused: can you help me?


Yea sure :slight_smile:.

Look at your return keyword, it's only going to return 1.5 to the console (based on where return is indented).


since we're adding the key of prices to total, this will equal 1.5, but we want the whole total. (8.5), so what do we do? We move the return keyword because we want to return the whole loop to total.

We want it to return all of the numbers in stock, so we should place it the same distance where we're looping.

Example skeleton:

def function(l):
     b = 1
     for m in l:
         if blah == blah:
            print "yay!"
     return b # <= look at how this will effect the output based on where our 'return' keyword is placed.


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