[Tutor] new function and a 3 line program not working

Tim Moffatt timmoffatt at cogeco.ca
Sun Nov 7 13:33:19 CET 2004


This is my first post, so HI all! and thank you for you help!

Below is a tutorial that I am trying to complete, but I can not duplicate this exercise in either the IDLE gui or command line. (ver 2.3.4)
Tutorial says do this

go to IDLE gui and define the new function

def newLine():
     print

then i open another IDLE window and write the 3 line program like in the tutorial 

print "First Line."
newLine()
print "Second Line."

save it as line.py

Now I am to see this output result upon running it 

First line.

Second line. 

But I dont get this, I get a First line." printed and a syntax error

>>> 
First Line.

Traceback (most recent call last):
File "C:/Python23/newLine.py", line 2, in -toplevel-
newLine()
NameError: name 'newLine' is not defined
>>> 


complete lesson below 


3.6 Adding new functions

So far, we have only been using the functions that come with Python, but it is also possible to add new functions. Creating new functions to solve your particular problems is one of the most useful things about a general-purpose programming language.

In the context of programming, a function is a named sequence of statements that performs a desired operation. This operation is specified in a function definition. The functions we have been using so far have been defined for us, and these definitions have been hidden. This is a good thing, because it allows us to use the functions without worrying about the details of their definitions.

The syntax for a function definition is:

def NAME( LIST OF PARAMETERS ):
STATEMENTS

You can make up any names you want for the functions you create, except that you can't use a name that is a Python keyword. The list of parameters specifies what information, if any, you have to provide in order to use the new function.

There can be any number of statements inside the function, but they have to be indented from the left margin. In the examples in this book, we will use an indentation of two spaces.

The first couple of functions we are going to write have no parameters, so the syntax looks like this:

def newLine():
print

This function is named newLine. The empty parentheses indicate that it has no parameters. It contains only a single statement, which outputs a newline character. (That's what happens when you use a printcommand without any arguments.)

The syntax for calling the new function is the same as the syntax for built-in functions:

print "First Line."
newLine()
print "Second Line."

The output of this program is:

First line.

Second line.

Notice the extra space between the two lines. What if we wanted more space between the lines? We could call the same function repeatedly:

print "First Line."
newLine()
newLine()
newLine()
print "Second Line."

Or we could write a new function named threeLines that prints three new lines:

def threeLines():
newLine()
newLine()
newLine()

print "First Line."
threeLines()
print "Second Line."

This function contains three statements, all of which are indented by two spaces. Since the next statement is not indented, Python knows that it is not part of the function.

You should notice a few things about this program:

1. You can call the same procedure repeatedly. In fact, it is quite common and useful to do so.
2. You can have one function call another function; in this case threeLines calls newLine.

So far, it may not be clear why it is worth the trouble to create all of these new functions. Actually, there are a lot of reasons, but this example demonstrates two:

* Creating a new function gives you an opportunity to name a group of statements. Functions can simplify a program by hiding a complex computation behind a single command and by using English words in place of arcane code.
* Creating a new function can make a program smaller by eliminating repetitive code. For example, a short way to print nine consecutive new lines is to call threeLines three times.

As an exercise, write a function called nineLines that uses threeLines to print nine blank lines. How would you print twenty-seven new lines? 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20041107/33d16176/attachment.html


More information about the Tutor mailing list