Results 1 to 6 of 6

Thread: Trying To Learn Python

  1. #1
    I am trying to learn something(hopefully) usefull from my time on the net.

    But I cannot seem to get past this part(which is pretty much at the begginning of the training) and make it work.

    I am using Python 2.3, the command module part and cannot do the following part:

    ___________________________________________________________________

    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."


    ____________________________________________________________

    Whenever I try to put in the commands such as:

    def newLine():
    print

    and hit enter, then the commands


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

    As soon as I hit enter after print "First Line." I get

    File "<stdin>", line 2
    print
    IndentationError: expected an indented block


    I cannot get it to perform the command it is supposed to.

    Am I supposed to enter the whole command on one line with some sort of separator or something?
    The instructions do not give any apparent reasons for this not to work.

    Any help is appreciated.

    TD

    Also, the link to the instructions is here.
    Peace of mind Findnot

    No time to work out? Try Folding instead.

  2. Software & Hardware   -   #2
    lynx's Avatar .
    Join Date
    Sep 2002
    Location
    Yorkshire, England
    Posts
    9,759
    If this works, it needs to look like this:

    def newLine():
    print

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



    Note the indentation.

    I believe the definition is ended when the indentation finished.

    Edit: that didn&#39;t work, so you may already be doing what I was trying to suggest.
    Edit2: If I try to quote your post I see you are doing it.
    .
    Political correctness is based on the principle that it's possible to pick up a turd by the clean end.

  3. Software & Hardware   -   #3
    Ok, when I type-

    def NewLine():

    and press enter it takes me down a line with 3 periods like so-

    ..._

    I then type print and hit enter which gives me the foillowing-

    File "<stdin>", line 2
    print
    IndentationError: expected an indented block
    (THIS SPACE HAS THE 3 ARROWS)

    I do not see the indentations you are refering to but when I did see them I believe I was using the-

    IDLE (python gui)

    I am sure whatever I am doing wrong is simple but it has me stumped(can you tell I&#39;m well on my way to another carreer? )

    Thanks, TD

    EDIT: Saw your edit after I posted
    Peace of mind Findnot

    No time to work out? Try Folding instead.

  4. Software & Hardware   -   #4
    4play's Avatar knob jockey
    Join Date
    Jan 2003
    Location
    London
    Age
    41
    Posts
    3,824
    just curious what guide are you using to teach yourself python. very good langauage to start with by the way.

  5. Software & Hardware   -   #5
    The tutorial is here.
    Peace of mind Findnot

    No time to work out? Try Folding instead.

  6. Software & Hardware   -   #6
    4play's Avatar knob jockey
    Join Date
    Jan 2003
    Location
    London
    Age
    41
    Posts
    3,824
    when writing python i prefer to use a file to store it in. for this you need to add a shebang at the front of all the scirpts you write. Just open up notepad write out your script and use

    #&#33;/usr/bin/python <-- this has to point to where ever you installed python and has to be at the very start of your script as well. that is the standard one to use but you will not have installed it there if your using windows.

    #&#33;/program files/python is more likely if your on windows.

    Code:
    #&#33;/usr/bin/python
    def newLine&#40;&#41;&#58;
    	print
    print &#34;First Line.&#34;
    newLine&#40;&#41;
    print &#34;Second Line.&#34;
    that code works fine for me just save it with a .py extension and run it from a command prompt.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •