A day at the supermarket not adding up

I’m having a similar issue to the chap(ess) here but they didn’t supply all their code so didn’t get a full answer.

When I try to run this the Academy puts in for 1 apple, 1 banana and 1 pear. I get the error message at the bottom saying:

calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 4 instead of the correct 7

It’s as if to my mind it hasn’t looped through to the pear for some reason.
My code is as below:

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 fruit in food:
    if stock[fruit] > 0:
      total += prices[fruit] # justs show the total for a banana    
      stock[fruit] -= 1
      return total

The return should not be inside the for block.

Well that’s obvious now! And makes perfect sense.

1 Like

Indentation is critical in Python, not just a style option. Blocks are identified by their level of indentation, and the type of error you just corrected is easy to make, and make often until we master it. You’re not alone in that regard. It is the most common error we see, here.

It’s all new to me, I’m used to SQL where it’s the devs enforcing the indentation rules! Thanks.

1 Like