12.Stocking Out indentation error?


#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/stocking-out-?action=lesson_resume

<In what way does your code behave incorrectly? Include ALL error messages.>

IndentationError: unindent does not match any outer indentation level

which is strange because I’ve looked at other solutions and my is identical

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

#2

This will add total twice. Should be either,

total = total + prices[x]

or,

total += prices[x]

We cannot see your indentation so don’t know what to advise other than check the consistency and validity of your indents.


#3

I see, how can I get the code to show properly here for future reference?


#4

A number of ways.

  1. Select the code and click </> in the tool tray above the post; or,
  2. Select the code and press Ctrl/Cmd + k; or,
  3. Preface the code with three backticks on their own line, then on a new line below the code type in three more.

The backtick is usually on the tilde (~) key.

```
Your code
```

To indicate the language so that perhaps language specific highlighting takes effect, type the language in lowercase immediately after the first set.

```python
# Your code
```

#5

Great, thank you. I’ve updated my OP but here is my code again, I simply do not see the error.

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

#6

Inconsistent indentation. Both lines should have the same indent.

Any size of indent is legal in Python, so long as it is consistent. A general rule is to use either two spaces, or four, and keep it consistent for readability.

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

or,

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

#7

Hmm, I am getting the same error

  File "python", line 23
    stock[x] -= 1
                ^
IndentationError: unindent does not match any outer indentation level
def compute_bill(food):
  total = 0
  for x in food:
    if stock[x] > 0:
    	total += prices[x]
      	stock[x] -= 1
  return total

#8

The easiest thing to do is remove all indentation, then go down the lines and add four, eight or twelve spaces, as apply (or 2, 4 or 6 spaces). Do not use TAB.


#9

Yep, it looks like it simply did not like 2 spaces as soon as I increased it to 4( I did not remove all indentation beforehand) the code worked. How strange… Thank you for your help


#10

2 posts were split to a new topic: I am getting an error


#11

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