import sysdef Factorial(n): # Return factorial result = 1 for i in range (1,n): result = result * i print "factorial is ",result return resultWhy?
Other posters are probably correct...there might be spaces mixed in with your tabs. Try doing a search & replace to replace all tabs with a few spaces.
You are watching: Unindent does not match any outer indentation level
import sysdef Factorial(n): # return factorial result = 1 for i in range (1,n): result = result * i print "factorial is ",result return resultprint Factorial(10)
Siha for this.)
For Sublime Text users:
Set Sublime Text to use tabs for indentation:View --> Indentation --> Convert Indentation to Tabs
Uncheck the Indent Using Spaces option as well in the same sub-menu above.This will immediately resolve this issue.
To easily check for problems with tabs/spaces you can actually do this:
python -m tabnanny yourfile.pyor you can just set up your editor correctly of course :-)
Are you sure you are not mixing tabs and spaces in your indentation white space? (That will cause that error.)
Note, it is recommended that you don"t use tabs in Python code. See the style guide. You should configure Notepad++ to insert spaces for tabs.
Whenever I"ve encountered this error, it"s because I"ve somehow mixed up tabs and spaces in my editor.
If you are using Vim, hit escape and then type
This auto indents everything and will clear up any spaces you have thrown in.
If you use Python"s IDLE editor you can do as it suggests in one of similar error messages:
1) select all, e.g. Ctrl + A
2) Go to Format -> Untabify Region
3) Double check your indenting is still correct, save and rerun your program.
I"m using Python 2.5.4
The line: result = result * i should be indented (it is the body of the for-loop).
Or - you have mixed space and tab characters
Packages > Whitespace > Convert Spaces to TabsThen check again your file indentation:
python -m tabnanny yourFile.pyorIf you use notepad++, do a "replace" with extended search mode to find \t and replace with four spaces.
Looks to be an indentation problem. You don"t have to match curly brackets in Python but you do have to match indentation levels.
The best way to prevent space/tab problems is to display invisible characters within your text editor. This will give you a quick way to prevent and/or resolve indentation-related errors.
Also, injecting copy-pasted code is a common source for this type of problem.
Using Visual studio code
If you are using vs code than, it will convert all mix Indentation to either space or tabs using this simple steps below.
press Ctrl + Shift + p
type indent using spaces
Just a addition. I had a similar problem with the both indentations in Notepad++.Unexcepted indentation
Outer Indentation Level
Go to ----> Search tab ----> tap on replace ----> hit the radio button Extended below ---> Now replace \t with four spaces
Go to ----> Search tab ----> tap on replace ----> hit the radio button Extended below ---> Now replace \n with nothing
I was using Jupyter notebook and tried almost all of the above solutions (adapting to my scenario) to no use. I then went line by line, deleted all spaces for each line and replaced with tab. That solved the issue.
For what its worth, my docstring was indented too much and this also throws the same error
class junk: """docstring is indented too much""" def fun(): return IndentationError: unindent does not match any outer indentation level
I"m using Sublime text in Ubuntu OS. To fix this issue go to
view -> Indentation -> convert indentation to tabs
It could be because the function above it is not indented the same way.i.e.
class a: def blah: print("Hello world") def blah1: print("Hello world")
Since I realize there"s no answer specific to spyder,I"ll add one: Basically, carefully look at your if statement and make sure all if, elif and else have the same spacing that is they"re in the same line at the start like so:
def your_choice(answer): if answer>5: print("You"re overaged") elif answer1: print("Welcome to the toddler"s club!") else: print("No worries mate!")
I am using Sublime Text 3 with a Flask project. I fixed the error using View > Indentation > Tab Width: 4 after unselected Indent Using Spaces
This is because there is a mix-up of both tabs and spaces.You can either remove all the spaces and replace them with tabs.
Or,Try writing this:
#!/usr/bin/python -ttat the beginning of the code. This line resolves any differences between tabs and spaces.
I had the same issue yesterday, it was indentation error, was using sublime text editor. took my hours trying to fix it and at the end I ended up copying the code into VI text editor and it just worked fine. ps python is too whitespace sensitive, make sure not to mix space and tab.
I had a function defined, but it did not had any content apart from its function comments...
def foo(bar): # Some awesome temporary comment. # But there is actually nothing in the function! # D"Oh!It yelled :
File "foobar.py", line 69 ^IndentationError: expected an indented block(note that the line the ^ mark points to is empty)
1: Just comment out the function
2: Add function comment
def foo(bar): "" Some awesome comment. This comment could be just one space.""3: Add line that does nothing
def foo(bar): 0In any case, make sure to make it obvious why it is an empty function - for yourself, or for your peers that will use your code
Improve this answer
edited Dec 10 "18 at 14:25
answered Dec 10 "18 at 14:07
4,5701010 gold badges3737 silver badges5757 bronze badges
Add a comment |
Firstly, just to remind you there is a logical error you better keep result=1 or else your output will be result=0 even after the loop runs.
See more: How Hard Is Dark Souls 2 Dark Chasm Of Old ?: Darksouls2 Dark Chasm Of Old
Secondly you can write it like this:
import sysdef Factorial(n): # Return factorial result = 0 for i in range (1,n): result = result * i print "factorial is ",result return resultLeaving a line will tell the python shell that the FOR statements have ended. If you have experience using the python shell then you can understand why we have to leave a line.
Improve this answer
answered Dec 25 "18 at 11:49
Faisal Ahmed FarooqFaisal Ahmed Farooq
12111 silver badge55 bronze badges
Add a comment |
I got this error even though I didn"t have any tabs in my code, and the reason was there was a superfluous closing parenthesis somewhere in my code. I should have figured this out earlier because it was messing up spaces before and after some equal signs... If you find anything off even after running Reformat code in your IDE (or manually running autopep8), make sure all your parentheses match, starting backwards from the weird spaces before/after the first equals sign.
Improve this answer
answered Jul 24 "20 at 7:52
52511 gold badge99 silver badges1111 bronze badges
Add a comment |
in my case, the problem was the configuration of pydev on Eclipse
Rui MartinsRui Martins
Imtiaz PabelImtiaz Pabel
Shahzaib Hayat Khan
Rene FergusonRene Ferguson