[Tutor] help on newbie program

Magnus Lyckå magnus@thinkware.se
Wed May 28 18:42:02 2003


Hi Nick, and welcome to Python. I hope you will enjoy it.
It's really a rather amazing tool to make thoughts turn
into action. :)

A wise man called Fred Brooks wrote about "the joys of the craft"
in his book "The Mythical Man-Month" almost 30 years ago. See
http://www.davar.net/PROGRAM/EXTRACTS/CRAFTJOY.HTM for his view
of why we like programming.

At 14:49 2003-05-28 -0600, Nick Jensen wrote:
>I have read every example and line of code up to and including the section 
>on lists, but I just cannot understand the last example in that section 
>named test.py.

Are you just reading this, or are you playing interactively
with the interpreter as you go along? Reading is good, but I
don't think there is a better way than to try to improvise at
the python interpreter based on what you read.

Few other languages makes it so simple to do that as Python.

The problem with *only* reading is that there is no way you
can really know how much you learnt. Does it work the way you
think, or do you only think you know how it works? You can never
know that, unless you try to modify the code to do something
different, based on the principles you think you understood,
and...fail. You need to push the limits...and get reality
checks...

Learning programming without really practicing, is like learning
electromagnetic fields and waves without doing the exercises
properly. I tried that, and the exam result was really humiliating. :)

Reading and thinking that you understand just won't cut it.

>I understand parts of it, but not all of it as a whole. I'm not able to 
>follow the flow of the program and whats happening at each step. Maybe I'm 
>asking too much, but was wondering if anyone could walk me through the 
>code and whats happening. By the way, I did send an email to Josh asking 
>this same question, but not sure if I'll hear back or not and thought I'd 
>ask here just in case. Here it is...

Have you tried single-stepping it in a debugger? In IDLE
for instance. Start IDLE and load the file in question,
Select Debug -> Debugger in the Python shell window to
open the "Debug Control", tick the "Source" checkbox,
activate the editor window with your source code, and
press Ctrl+F5 to start the program. Then use the "Step"
button in the Debug Control to advance one code line a
at time. This will let you follow the way the code is
executed. You will also be able to see the current values
of the objects that are in the current local and global
(it you choose so) scopes. You might get into other
pieces of code during the debugging, at least when the
program asks you for a question. At that point you can
just press the "Out" button in Debug Control to run
until you leave that code. You might also need to manually
switch to the Python shell window to be able to answer
the question when it's time for that. (If the "Step"
button is grey and you don't know understand why you
can't continue, it's probably because you have to answer
a question. Activate the Python shell window, type your
answer followed by pressing the [Enter]-key. (Actually,
now that I tried, I eventually got stuck in the debugger.
Maybe the debugger needs debugging? Anyway, I had already
answered the third question by that time, so I had followed
most of the code.)

Returning to your woes:

What is it you don't understand? We don't want to spend
our own and your time explaining things you already know! ;)

Do you understand what a function is, and how defining a
function is different from calling a function?

Do you understand how functions return values?

Do you understand the concept of scopes and namespaces?

Do you understand the distinction between objects and the
names that refer to ojects?

Do you understand these questions? ;)

>run_test(get_questions())

Do you understand that the above is eqivalent to the code below?

questions = get_questions()
run_test(questions)


--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program