need help regarding compilation

Peter Hansen peter at engcorp.com
Thu Feb 23 12:19:04 EST 2006


fidlee at gmail.com wrote:
> i am new to learning jython...
> 
> 
> i just tried compiling a small piece of code that is given below:
> 
> def fac(x)
>  if x<=1:return 1
>  return x*fac(x-1)

This is invalid Python syntax.  Have you gone through the Python 
tutorial yet?  Doing so is probably a good idea if you haven't.

The problem is that "def" statements must be followed with a colon, like so:

def fac(x):
     if x <= 1:
         return 1
     return x * fac(x-1)

(Note the colon after "def fac(x)".  I've also taken the liberty of 
reformatting the code slightly make it more readable, but that isn't 
necessary to avoid the SyntaxError you were getting.)

-Peter




More information about the Python-list mailing list