1

i hv code like this

# -*- coding: utf-8 -*-

class OP(object):

  def RECEIVE_MESSAGE(self):
      print("done")


  def NOTIFIED_INVITE_INTO_GROUP(self):
    print("done")

but when i run this i got some error

Traceback (most recent call last):
File "run.py", line 2, in <module>
  from van import ERVAN
File "/home/server/.pub/van.py", line 4, in <module>
  from op import OP
File "/home/server/.pub/op.py", line 9
  def NOTIFIED_INVITE_INTO_GROUP(self):
                                    ^
IndentationError: unindent does not match any outer indentation level

any solution for this? its just 10 lines but blowing my mind

Ervan
  • 81
  • 2
  • 9
  • In your code looks like the first function has four spaces indentation and the second only two. If you pasted it from your file, and did not make a typo here, this could be the problem. – desoares Apr 21 '18 at 02:46
  • you can use "tab" too, is basically 4 spaces and no need to count the spaces (this is how I use and is most compatible with the code even from py2.7) – n1tk Apr 21 '18 at 02:52
  • But in Python 3+ you're not allowed to mix spaces and tabs - so choose one or the other and stick to it. – Richard Inglis Apr 21 '18 at 02:56

1 Answers1

5

Here's your code with spaces replaced by dots:

#.-*-.coding:.utf-8.-*-

class.OP(object):

..def.RECEIVE_MESSAGE(self):
......print("done")

..def.NOTIFIED_INVITE_INTO_GROUP(self):
....print("done")

As you can see, the first print("done") statement is indented by 6 spaces - change it to 4 to fix the problem.

Better still, change all indents so they are multiples of 4 spaces (ie 0, 4, 8, 12 etc), as recommended by PEP 8

#.-*-.coding:.utf-8.-*-

class.OP(object):
....def.RECEIVE_MESSAGE(self):
........print("done")

....def.NOTIFIED_INVITE_INTO_GROUP(self):
........print("done")

More detail, from Python: Myths about Indentation

How does the compiler parse the indentation? The parsing is well-defined and quite simple. Basically, changes to the indentation level are inserted as tokens into the token stream.

The lexical analyzer (tokenizer) uses a stack to store indentation levels. At the beginning, the stack contains just the value 0, which is the leftmost position. Whenever a nested block begins, the new indentation level is pushed on the stack, and an "INDENT" token is inserted into the token stream which is passed to the parser. There can never be more than one "INDENT" token in a row.

When a line is encountered with a smaller indentation level, values are popped from the stack until a value is on top which is equal to the new indentation level (if none is found, a syntax error occurs).

Richard Inglis
  • 5,888
  • 2
  • 33
  • 37
  • 1
    also per PEP8 naming convention: Function name should be lowercase. – n1tk Apr 21 '18 at 02:47
  • no problem - don't forget, you can 'accept' an answer by clicking the green checkbox - that will encourage others to help you with your next question :) – Richard Inglis Apr 21 '18 at 03:01