On May 5, 1:43 pm, notbob <[EMAIL PROTECTED]> wrote: > On 2008-05-04, notbob <[EMAIL PROTECTED]> 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?
Well, it prints whatever bruce is. > 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> Looks like you didn't call it right. Even though myfirstfunction doesn't take any parameters, you still have to use parentheses. You must invoke it by myfirstfunction(). Without the parenthese, you're just printing the location of the function. This allows functions to call functions, so that you can take a list and apply myfirstfunction to every item in the list even though myfirstfunction was designed to process only a single item. > ....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. Should be TWO underscores, '.__doc__', not '._doc_'. Such doc strings can be very useful. For example: >>> import collatz_functions >>> help(collatz_functions.collatz) Help on function collatz in module collatz_functions: collatz(a, p) 3x+1 sequencer, no loop detection collatz(a,p) a: starting value p: print options (binary) bit 0 print even numbers (turns off power division) bit 1 print odd numbers bit 2 print debug (if any) returns: CollatzSequenceParameters [R1count, R2count] Notice collatz() takes two arguments, but I left off the parentheses and arguments when using the help() function. That's an example of using the function location. > ****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/. Actually, I'm not sure where you should put it. I put mine in the Lib directory. > ****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. You use "from" to specify what parts of the module to import, not specify where the module is located. For instance, I could have said above from collatz_functions import collatz and just that function would have been loaded and I could call it directly, collatz(x,y) instead of collatz_functions.collatz(x,y). > ****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? Yes, once you define print_twice(bruce), you have to call it with something. print_twice(5) prints 5 5 print_twice('fred') prints fred fred print_twice(bruce) is an error if you haven't defined a variable named bruce > 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. You have to use parentheses and enclose some legal entity when you call it. > ****ME**** > > ---------- > > I feel dumber'n a bag 0' hammers and in need of clarification. > > Thank you > nb -- http://mail.python.org/mailman/listinfo/python-list