[Tutor] syntax error

John Fouhy john at fouhy.net
Tue Aug 1 00:34:59 CEST 2006


On 01/08/06, Christopher Spears <cspears2002 at yahoo.com> wrote:
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>  File "linkedPriorityQueue.py", line 27
>    else node.cargo <= self.head.cargo:
>            ^
> SyntaxError: invalid syntax
>
> I am not sure what this means.  Everything is spelled
> correctly.

An 'else' clause doesn't take a condition.  The general shape of an if
statement is:

if [condition]:
    do something
elif [another condition]:
    do something else
else:
    do other things

The 'else' clause is the catchall for anything that doesn't match one
of the preceding conditions, so it doesn't have its own condition.

What I often like to do is to use a comment to indicate what is true
in the 'else' branch.  eg:

               else:  # node.cargo <= self.head.cargo
                       self.last.next = node
                       self.last = node

HTH!

-- 
John.


More information about the Tutor mailing list