[python] function is not defined error in Python

I am trying to define a basic function in python but I always get the following error when I run a simple test program;

>>> pyth_test(1, 2)

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    pyth_test(1, 2)
NameError: name 'pyth_test' is not defined

Here is the code I am using for this function;

def pyth_test (x1, x2):
    print x1 + x2

UPDATE: I have the script called pyth.py open, and then I am typing in pyth_test(1,2) in the interpreter when it gives the error.

Thanks for the help. (I apologize for the basic question, I've never programmed before and am trying to learn Python as a hobby)


import sys
sys.path.append ('/Users/clanc/Documents/Development/')
import test


printline()



## (the function printline in the test.py file
##def printline():
##   print "I am working"

This question is related to python function

The answer is


if working with IDLE installed version of Python

>>>def any(a,b):
...    print(a+b)
...
>>>any(1,2)
3

It would help if you showed the code you are using for the simple test program. Put directly into the interpreter this seems to work.

>>> def pyth_test (x1, x2):
...     print x1 + x2
... 
>>> pyth_test(1, 2)
3
>>> 

In python functions aren't accessible magically from everywhere (like they are in say, php). They have to be declared first. So this will work:

def pyth_test (x1, x2):
    print x1 + x2

pyth_test(1, 2)

But this won't:

pyth_test(1, 2)

def pyth_test (x1, x2):
    print x1 + x2

It works for me:

>>> def pyth_test (x1, x2):
...     print x1 + x2
...
>>> pyth_test(1,2)
3

Make sure you define the function before you call it.