[Tutor] Re: Tutor Digest, Vol 9, Issue 32
Jim Kelly
jkmacman at yahoo.com
Thu Nov 11 17:10:45 CET 2004
why not try this
print "First Line.\n"
print "Second Line."
----------------------------------------------------------------------
>
> Message: 1
> Date: Wed, 10 Nov 2004 08:24:13 -0500
> From: "SunDragon" <sundragon at cogeco.ca>
> Subject: [Tutor] New function and 3 line program not
> working?
> To: <tutor at python.org>
> Message-ID:
> <00d401c4c728$92566010$6500a8c0 at cdca1h1bro1>
> Content-Type: text/plain; charset="iso-8859-1"
>
> 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 to do this....
>
> define the new function---- I go to IDLE gui and
> define the function....
>
> def newLine():
> print
>
>
> write the 3 line program---- I go open another IDLE
> window and like in the tutorial type this 3 line
> program....
>
> print "First Line."
> newLine()
> print "Second Line."
>
> I save it as line.py
>
> Now I am to see this output result upon running
> it....
>
> First line.
>
> Second line.
>
> But I dont get the output I should, I get the 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
> >>>
>
> What have I inputed or done wrong?
>
>
> 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.
>
=== message truncated ===
__________________________________
Do you Yahoo!?
Check out the new Yahoo! Front Page.
www.yahoo.com
More information about the Tutor
mailing list