#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    3
    Rep Power
    0

    Question syntax error on compiling


    hi ,
    i am new to Linux and learning Python

    I get a syntax error for this line of code

    def fib(n): # write Fibonacci series up to n

    i used Emacs as the editor on a Redhat ver8.0 OS.

    any help is useful , thanks

    --vissen
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Please post the rest of your code too.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    3
    Rep Power
    0
    it is one of the examples given in the python tutorial by Guido Rossum which i used to learn. so here is the link
    http://www.python.org/doc/current/tut/node6.html
    please refer section 4.6 Defining Functions

    i just copied this in emacs editor to check if it compiles when i got syntax error.

    Code:
    def fib(n):    # write Fibonacci series up to n
         """Print a Fibonacci series up to n."""
         a, b = 0, 1
         while b < n:
             print b,
             a, b = b, a+b
    Last edited by netytan; February 16th, 2004 at 05:55 AM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    3
    Rep Power
    0
    also tried this unsuccesfully

    Code:
    def fib2(n): # return Fibonacci series up to n
         """Return a list containing the Fibonacci series up to n."""
         result = []
         a, b = 0, 1
         while b < n:
             result.append(b)    # see below
            a, b = b, a+b
         return result
    -------
    any ideas ??
    Last edited by netytan; February 16th, 2004 at 06:00 AM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24
    Is your code properly indented, like in the example?

    Code:
    >>> def fib(n):    # write Fibonacci series up to n
    ...     """Print a Fibonacci series up to n."""
    ...     a, b = 0, 1
    ...     while b < n:
    ...         print b,
    ...         a, b = b, a+b
    ...
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You're first example runs fine, no problem at all there. The second version is suffering from indentation arrors. if you insure that you're using 4 spaces for each indentation level it should work perfectly i.e.

    Code:
    def fib2(n):
        """Return a list containing the Fibonacci series up to n."""
        result = []
        a, b = 0, 1
        while b < n:
            result.append(b)
            a, b = b, a + b
        return result
        
    print fib2(10)
    Mark.
    programming language development: www.netytan.com Hula

Similar Threads

  1. syntax error : identifer 'Hole'
    By bootrecker in forum C Programming
    Replies: 2
    Last Post: February 2nd, 2004, 08:08 AM
  2. syntax error
    By DQSJR in forum MySQL Help
    Replies: 2
    Last Post: December 31st, 2003, 01:28 PM
  3. mysql syntax error
    By TweetyPie in forum MySQL Help
    Replies: 2
    Last Post: July 25th, 2003, 10:22 AM
  4. Problem compiling in Cygwin
    By dtm78 in forum C Programming
    Replies: 1
    Last Post: February 24th, 2003, 05:06 AM
  5. Declaration syntax error when compiling
    By nate in forum C Programming
    Replies: 1
    Last Post: January 4th, 2003, 07:30 PM

IMN logo majestic logo threadwatch logo seochat tools logo