Indentation Error in Python

2019-01-09 13:37发布

I can't compile because of this part in my code:

if command == 'HOWMANY':
    opcodegroupr = "A0"
    opcoder = "85"
elif command == 'IDENTIFY':
    opcodegroupr = "A0"
    opcoder = "81"

I have this error:

Sorry: IndentationError: ('unindent does not match any outer indentation level', ('wsn.py', 1016, 30, "\t\telif command == 'IDENTIFY':\n"))

But I don't see any indentation error. What can be the problem?

7条回答
唯我独甜
2楼-- · 2019-01-09 14:05

In Notepad++

View --->Show Symbols --->Show White Spaces and Tabs(select)

replace all tabs with spaces.

查看更多
Emotional °昔
3楼-- · 2019-01-09 14:07

It happened to me also but i got the problem. I was using an indentation of 5 spaces but when pressed tab, it used to put four space indent. So i think you should just use one thing; i.e. either tab button to add indent or spaces. And an ideal one is of 4 spaces. I found IntelliJ to be very useful for these things.

查看更多
Evening l夕情丶
4楼-- · 2019-01-09 14:08

Did you maybe use some <tab> instead of spaces?

Try remove all the spaces before the code and readd them using <space> characters, just to be sure it's not a <tab>.

查看更多
甜甜的少女心
5楼-- · 2019-01-09 14:10

You are mixing tabs and spaces.

Find the exact location with:

python -tt yourscript.py

and replace all tabs with spaces. You really want to configure your text editor to only insert spaces for tabs as well.

查看更多
放我归山
6楼-- · 2019-01-09 14:14

In doubt change your editor to make tabs and spaces visible. It is also a very good idea to have the editor resolve all tabs to 4 spaces.

查看更多
再贱就再见
7楼-- · 2019-01-09 14:22

Indentation Error generally occurs when there is inconsistency in the code means somewhere we have used spaces and somewhere tabs for indentation. I have got a very nice solution to this, just open your code in sublime text editor and find 'Tab Size' in bottom right corner of Sublime Text Editor and click it. Now select either

'Convert Indentation to Spaces'

OR

'Convert Indentation to Tabs'

Your code will work in either case.

查看更多
登录 后发表回答