12. Stocking out - Indentation Problem


#1

hi guys, I have a problem about the indentation in this practice.

If my code is like this, "return total" is aligned with "if". Then it will 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". I think aligning with "if" can show that it is in the same block with the loop. But according to the reminder, it doesn't.

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

Any idea? Many thanks.


#2

I'm pretty sure the question stated that

For now, go ahead and ignore whether or not the item you're billing for is in stock.

Which you ignored by putting that if stock[i] >0 and stock[i] = stock[i]-1 in your function.
This causes your total to be 0.

After you remove those, line the return statement with the for loop.


#3

I think I may tag this problem in the wrong place. Please find the right answer above. My problem is how indentation works here. You see, my error here is I indent more than needed.


#4

Oh, ok. you're on that question.

From what I see, the first code you gave me, the return isn't exactly lined up with the if statement. Make sure it is.


#5

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