I need help on stocking out


#1

Error: Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in {'orange': 32, 'pear': 15, 'banana': 5, 'apple': 0} instead of the correct 7

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

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


#2

You want to return stock or calculated total value? :slight_smile:


#3

calculate the total value


#4

So you have to change this:

return stock

to:

return total

right?


#5

when I return total is says :
Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 4 instead of the correct 7


#6

Ok, that's a progress :slight_smile: Now we have to check one more thing - indentation levels. Your return statement is probably inside the for loop.

Please post your code with proper formatting. Select your code and click </> icon in the toolbar above the text area.


#7

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

#8

I still get
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


#9

I was right! return is inside the for loop :slight_smile:

You have to decrease level of indentation:

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

Why is this important?

return statement terminates execution of the function.

So let's say that we have the list ["banana", "apple"]. We enter the for loop and now x = "banana". We add price of banana to total and we return total. This is the end of the function.

What about apple? Price of apple will not included be included in the total. That is why return must be placed after for loop.


#10

It returns
File "python", line 24
SyntaxError: 'return' outside function


#11

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

#12

Now it truly is outside the function.

It should look just how I posted it earlier:

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

It is inside the function and it's not in the for loop. Perfect situation.


#13

Wow now I have to look at how I indent more often. Thanks you were a big help


#14

You're very welcome :slight_smile:

I don't want to be mean, please not be offended, but I think that it might be good for you to take a look at the earlier sections of this course. Just to get a better understanding of blocks and functions.


#15

Why do we need to decrease the stock of x by 1???


#16

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

We have to keep track of how many fruits we have in the stock to know how many we can sell.


#17

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