PDA

View Full Version : Trying To Learn Python



tracydani
06-15-2004, 09:11 PM
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. (http://ibiblio.org/obp/thinkCS/python/english/chap03.htm)

lynx
06-15-2004, 09:38 PM
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.

tracydani
06-15-2004, 09:46 PM
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? :lol: )

Thanks, TD

EDIT: Saw your edit after I posted :)

4play
06-16-2004, 12:19 PM
just curious what guide are you using to teach yourself python. very good langauage to start with by the way. ;)

tracydani
06-16-2004, 06:16 PM
The tutorial is here. (http://ibiblio.org/obp/thinkCS/python/english/index.htm)

4play
06-17-2004, 01:35 AM
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.



#&#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.