3

I get the following indentation error

>>> number = 35
>>> if number == 35:
        print 'true'
    else:

  File "<pyshell#130>", line 3
    else:

^
IndentationError: unindent does not match any outer indentation level
>>> 

This happens only on IDLE and not on command line. I am using Python 2.6.4 on Windows XP. I have searched online, but not been able to find any answer as to why I get this error. Thankful for any help.

And to add - The code may not look properly indented when copied and pasted. But it was properly indented on the IDLE interface.

2
  • check if you are mixing tabs and spaces.. Commented Feb 29, 2012 at 11:20
  • 1
    It's clearly a bug in IDLE in Windows, OP - I have a file that has no dedents, no tabs, all indents EXACTLY right (4 spaces), and I get this error sporadically. File runs precisely as expected on Wing and on the command line: similarly , IDLE often screws up File->Open and arbitrarily replicates parts of the file. Don't use IDLE, is my basic point: it's the Windows 8 of quasi-IDEs. Commented Oct 17, 2014 at 3:04

7 Answers 7

2
>>> number = 35
>>> if number == 35:
...     print 'true'
... else:
...     print 'false'
... 
true
>>>

indent your code properly and manually

Sign up to request clarification or add additional context in comments.

Comments

1

Any block of code (started just after :) should have indent at least one space (or tab) more, than the parent one. In your example:

if number == 35:
    print 'true'
else:
    ...

But not:

if number == 35:
print 'true'
else:
...

Comments

1

Might not the way you wanted but it is clearly simple and effective:

Instead of this:

>>> number = 35
>>> if number == 35:
    print 'true'
else:

You can always check conditions on python idle without using if keyword such as:

>>> number = 35
>>> number == 35
True

Comments

0

You should define indent manually

>>> number = 35
>>> if number == 35:
...     print 'true'
... else:
...     pass
... 
true

Comments

0

For me this worked:

>>> if number == 35:
....print 'true'
else:
....print 'false'

true

Looks like the '>>> ' is not counted in indentation. I am using 2.7.13

Comments

0

Correct, don't indent the else statement. I am using version 3.9

if number == 35:
    print("true")
else:
    print("false")

Comments

0

I am sorry, but the answer of Jan 13 else does look indented, based on how my version of IDLE3 looks with version 3.9.7. I was having all sorts of problems until I came here and finally got put on the right track. Manual indenting/non-indenting while letting IDLE3 drop indent otherwise is the key for me anyway. This is how I did things:

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.