[Tutor] IndentationError:

Dario Lopez-Kästen cl2dlope at gmail.com
Wed Nov 16 09:06:36 CET 2011


The indentation is indeed off:

Original code:

def PlotPathway(list1):
   for i in range(len(list1)):
       for j in range(len(list1[i])-1):
                   if list1[i][j] != list1[i][j+1]:
                       g.add_edge(list1[i][j], list1[i][j+1])

               if list1[i][j]<=42:
                   g.node_attr.update(color='deepskyblue',style='filled')
               if list1[i][j] > 42:
                   g.node_attr.update(color='green',style='filled')

What I think you meant:

def PlotPathway(list1):
    for i in range(len(list1)):
        for j in range(len(list1[i])-1):
            if list1[i][j] != list1[i][j+1]:
                g.add_edge(list1[i][j], list1[i][j+1])

            if list1[i][j]<=42:
                g.node_attr.update(color='deepskyblue',style='filled')
            if list1[i][j] > 42:
                g.node_attr.update(color='green',style='filled')


Notice that I *consistently* use 4 spaces, and *only spaces, not tabs,* for
each indentation level. In your code (assuming the copy paste I did was
correct) I could see a mixture in the number of spaces for each indentation
level.

The error was the python interpreted the second and third if statements as
being not properly indented, becuase of the lack of consitency:


   1. they did not align with the first if statement in side the for loop
   2. the did not align with the for-loop either, so there could not be
   intrepreted as being on the same level as the for loop.


Hope this makes sense and helps!

Best regards,

/dario

On Wed, Nov 16, 2011 at 8:52 AM, lina <lina.lastname at gmail.com> wrote:

> Why it keeps on complaining:
>
> $ python plot-pathway.py
>  File "plot-pathway.py", line 35
>    if list1[i][j]<=42:
>                      ^
> IndentationError: unindent does not match any outer indentation level
>
>
> def PlotPathway(list1):
>    for i in range(len(list1)):
>        for j in range(len(list1[i])-1):
>                    if list1[i][j] != list1[i][j+1]:
>                        g.add_edge(list1[i][j], list1[i][j+1])
>
>                if list1[i][j]<=42:
>                    g.node_attr.update(color='deepskyblue',style='filled')
>                if list1[i][j] > 42:
>                    g.node_attr.update(color='green',style='filled')
>
> I checked the indentation very carefully, seems no problems.
>
> really no clue,
>
> Thanks with best regards,
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111116/b612dbad/attachment-0001.html>


More information about the Tutor mailing list