Are rank noobs tolerated, here?

notbob notbob at nothome.com
Mon May 5 14:43:11 EDT 2008


On 2008-05-04, notbob <notbob at nothome.com> wrote:

> I'm trying to learn how to program.  I'm using:
>   
> How to Think Like a Computer Scientist
>
> Learning with Python
> 2nd Edition

http://openbookproject.net//thinkCSpy/index.xhtml

OK then, using the above, I get everything up till chap 3 and functions and
then it all falls apart.  I try using his code and nothing.  I'm running
vers 2.5.1 on slackware 12.  Here's what I don't get:

----------

"Here is an example of a user-defined function that has a parameter:


def print_twice(bruce):
    print bruce, bruce
    
This function takes a single argument and assigns it to the parameter named
bruce.  The value of the parameter (at this point we have no idea what it
will be) is printed twice, followed by a newline.  The name bruce was chosen
to suggest that the name you give a parameter is up to you, but in general,
you want to choose something more illustrative than bruce.

****ME****
is this just an example of how the def should be written and it doesn't
really do anthing... yet?  I read another newb explanation here:
http://www.codepedia.com/1/BeginnersGuideToPython_Functions
...and it doesn't work, either.  I define myfirstfunction in the pyth editor
and give the command print myfirstfuntion and I get back this:
<function myfirstfunction at 0xb7b9c994>
....when I add the ._doc_, it get this:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute '_doc_'
....so, there is a no go there, too.
****ME****


The interactive Python shell provides us with a convenient way to test
our functions.  We can use the import statement to bring the functions
we have defined in a script into the interpreter session.  To see how this
works, assume the print_twice function is defined in a script
named chap03.py.  We can now test it interactively by
importing it into our Python shell session:

****ME****
ok, I try and follow the above, but where is he getting the script?  So, I
make a script called chap03.py and put it in ~/pyth/chap03/.  
****ME****

>>> from chap03 import *
>>> print_twice('Spam')
Spam Spam
>>> print_twice(5)
5 5
>>> print_twice(3.14159)
3.14159 3.14159

****ME****
I then try an do the import thing above, but the pyth ed accepts none of it.
not from:
from ~/pyth/chap03/ import *       
from chap03 import *            #taken from ~/pyth/
....nor from any other configuration I can imagine, so that "from" command
makes no sense.  
****ME****


In a function call, the value of the argument is assigned to the
corresponding parameter in the function definition.  In effect, it is if
bruce = 'Spam' is executed when print_twice('Spam')
is called, bruce = 5 in print_twice(5), and
bruce = 3.14159 in print_twice(3.14159)."

****ME****
OK, I'm totally lost.  Neither the above or the other link works or make any
sense at all.  Is the above print_twice(5), etc, supposed to work like the
original print_twice(bruce) function (which still doesn't!), just with a
different parameter?  Call me stupid, but I'd sure like to get past this.
How am I supposed to get the original def to work?  I've yet to figure out
how to get plain ol':
bruce bruce
Yes, I've indented 4 spaces where I should.  
****ME****

----------

I feel dumber'n a bag 0' hammers and in need of clarification.

Thank you
nb



More information about the Python-list mailing list