Help me on function definition

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Mar 29 03:14:43 CET 2008


On Sat, 29 Mar 2008 01:47:21 +0000, aeneng wrote:

> Hello everyone,
> 
> I am just starting to use python in numerical cacluation. I need you to
> help me to see what's wrong with the following piece of codes, which
> computes the cross product of two vectors and returns the result. u and
> v are two 3x1 matrix.
> 
> when I import the function, error message show like this
>>>> import cross
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "cross.py", line 8
>     ppp2=u[2]*v[0]-u[0]*v[2]
>     ^
> SyntaxError: invalid syntax
> 
> WHAT IS WRONG WITH MY CODE?


You don't win any points by writing the most unreadable code can you. A 
little bit of white space makes the code so much easier to read:

ppp2 = u[2]*v[0] - u[0]*v[2]


But that's not the problem.

Your code mixes spaces and tabs for indentation. Spaces are good; tabs 
are good; both together will eventually lead to disaster.

But that's not your problem either.

Your REAL problem is that the error you are reporting is NOT the error 
your code gives. Hint: if your code raises an error, you must copy the 
code that actually raises an error, not different code with different 
errors.


The code you post reports this error:


>>> import cross
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "cross.py", line 1
    def cross(u,v)
                 ^
SyntaxError: invalid syntax


After fixing that fault (put a colon after the function definition), your 
code imports correctly.

My *guess* is that in your actual code that fails, you have forgotten to 
close a bracket or brace in line 7 (not 8).



-- 
Steven



More information about the Python-list mailing list