def LCS(word_list1, word_list2):
m = len(word_list1)
n = len(word_list2)
print m
print n
# An (m+1) times (n+1) matriword_list1
C = [[0] * (n+1) for i in range(m+1)] # IndentationError: unindent does not match any outer indentation level
print C
i=0
j=0
for word in word_list1:
j=0
for word in word_list2:
if word_list1[i-1] == word_list2[j-1]:
C[i][j] = C[i-1][j-1] + 1
else:
C[i][j] = max(word_list1(C[i][j-1], C[i-1][j]))
j+=1
i+=1
return C

- 62,466
- 11
- 102
- 153

- 45
- 1
- 2
- 7
-
3Wow, python interpretor already answered your question "IndentationError: unindent does not match" – Anurag Uniyal Oct 03 '10 at 08:20
-
2a good Python IDE would highlight wrong indentation while you type the code. Check out [What IDE to use for Python?](http://stackoverflow.com/questions/81584/what-ide-to-use-for-python) – Nick Dandoulakis Oct 03 '10 at 08:34
-
3I spent two minutes wondering how the commented line is indented incorrectly. Geez, why did someone correct the error *in the question?* Without any notice and all? – Oct 03 '10 at 09:13
3 Answers
It's hard to answer a question on why indentation is incorrect when the question keeps being edited and the indentation corrected.
However, I suggest you read PEP8 before writing any more Python code and avoid mixing tabs and spaces. This would explain why you still see an IndentationError
on line seven after you have corrected the indentation.
I also recommend that you try to run your script using the '-tt
' command-line option to determine when you accidentally mix tabs and spaces. Of course any decent editor will be able to highlight tabs versus spaces (such as Vim's 'list'
option).
-
I ran into an issue editing a file with a different machine with different VIM settings. "list" helped me figured out that the original machine inserted tabs, but the other spaces. Setting "noexpandtab" on my new machine fixed it. Adding a vim modeline to my file helped me make sure it worked the same everywhere. – Brad Feb 13 '14 at 15:31
These two lines
C = [[0] * (n+1) for i in range(m+1)] # IndentationError: unindent does not match any outer indentation level
print C
should be indented at the same level. I.e.:
C = [[0] * (n+1) for i in range(m+1)]
print C
Update
Op has corrected the above problem. I checked the code and the error is elsewhere now:
for word in word_list2:
if word_list1[i-1] == word_list2[j-1]:
C[i][j] = C[i-1][j-1] + 1
else:
C[i][j] = max(word_list1(C[i][j-1], C[i-1][j]))
j+=1
Should be:
for word in word_list2:
# These lines have been indented extra.
if word_list1[i-1] == word_list2[j-1]:
C[i][j] = C[i-1][j-1] + 1
else:
C[i][j] = max(word_list1(C[i][j-1], C[i-1][j]))
j+=1

- 72,339
- 21
- 134
- 141
-
@Aneeshia. It was not showing the same error. it was showing a _different_ error because you had two _different_ errors to begin with. – aaronasterling Oct 03 '10 at 08:35
-
@Aneeshia: I am unable to reproduce your error. Why don't you make sure that your indentation is consistent? Use spaces everywhere or tabs everywhere but DON'T mix and match. – Manoj Govindan Oct 03 '10 at 12:07
I think its due use of mixed indentation - Tabs and Spaces
Recommendation: Use 4 spaces per indentation level,
Never mix tabs and spaces.
Maximum Line Length: 80
Check your Python - Editor's Setting,
GEdit:
From Tools -> Preferences -> Editor -> Tab Width = 4 and use Spaces instead of Tabs
Eclipse:
use Pydev - http://pydev.org
VIM:
use following vim settings - for vim editor
:set tabstop=4 expandtab shiftwidth=4 softtabstop=4

- 13,637
- 22
- 74
- 104