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

    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Problem with Python 2.7 on Eclipse (defining function)


    first off, this is what i have written:

    def fibonacci(n):
    terms=[0,1]
    i=2
    if n<0 or n>100000:
    print "error"
    else:
    while i<=n:
    terms.append(terms[i-1]+terms[i-2])
    i=i+1
    return terms[n]

    clearly, i define fibonacci here considering thats what the purpose of that whole thing is. However, when i try to use fibonacci( ) in the console it says NameError: name 'fibonacci' is not defined

    what is wrong? did i not define it correctly? please help.

    btw, i am very new to program so a simple explanation would be greatly appreciated (nothing too complicated please)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    I don't know what "on Eclipse" means. It might be important.

    Suppose I put your program into the file p.py in my current directory. This is how I start python, load your code, and run it:
    Code:
    $ python
    Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import p
    >>> p.fibonacci(8)
    21
    >>>
    Or you might heed this advice:
    June 27th, 2012, 10:33 AM

    Dietrich
    Contributing User


    Join Date: Feb 2005
    Posts: 352
    Time spent in forums: 2 Days 12 h 26 m 48 sec
    Reputation Power: 26
    You would be much better off to paste the code into the editor of an IDE (Integrated Development Environment) like IDLE and run (press F5 key) it from there.

    You can find IDLE for instance in:
    C:\Python27\Lib\idlelib\idle.py
    Comments on this post
    cheekychives agrees: Very helpful.
    __________________
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
    Last edited by Dietrich : June 27th, 2012 at 10:35 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0
    unfortunately, none of that help. i appreciate the effort though. if it helps to understand my problem, eclipse is a software used as an environment for writing programs in various different languages including python. i would much rather use another environment but it seems i am limited considering i am using a mac book pro. if you have suggestions of good environments i could use that would work on my mac, that may be just as good as helping me with my original problem. however, since i have eclipse installed already, i would prefer the former.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    I'm quite sure you've searched the internet for
    use python on eclipse
    ?
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo