[Tutor] New function and 3 line program not working?
alan.gauld at freenet.co.uk
Thu Nov 11 00:46:44 CET 2004
> define the new function---- I go to IDLE gui and define the
> def newLine():
> write the 3 line program----
> I go open another IDLE window and like in the tutorial type this 3
And here is the mistake, you must type the program *in the same
as the function definition. The reason is that Pyton only knows about
your function in the session where you defined it, once you go to a
new session that instance of Python knows nothing about your function.
> print "First Line."
> print "Second Line."
>I save it as line.py
BUT you are saving it as a file not, as I first thought using a
second interactive >>> prompt. Now if you did the same thing with
your function definition, saving it into newline.py say, you could
add a line
from newline import newLine
at the start of your line.py file and it would work. This is because
by importing the function your new instance of Python now knows about
the function definition.
> Traceback (most recent call last):
> File "C:/Python23/newLine.py", line 2, in -toplevel-
> NameError: name 'newLine' is not defined
That's Python's way of telling you what I just said, the instance of
Python running your line.py file cannot see the function definition
More etails omn using functions and modsules in my tutorial topic
of the same name.
Author of the Learn to Program web tutor
More information about the Tutor