[Tutor] first steps

Karl Pflästerer sigurd at 12move.de
Thu Apr 1 19:41:26 EST 2004


On 28 Mar 2004, peter hodgson <- py at humnet.ucla.edu wrote:

> A. I DON'T UNDERSTAND THIS:

> Here is another example of variable usage:

> a = 1
> print a
> a = a + 1
[...]

> Even if it is the same variable on both sides [OF THE '='] the
> computer still reads it as: First find out the data to store [RIGHT
> SIDE OF THE '='?] and than [THEN] find out where the data goes [PRINT
> COMMAND?].

The `=' sign is misleading; most people who have a feeling for
mathematics stumble first on this.  Some languages use for assignment a
different sign to not confuse people.

You uses the right words.  If you assign a value you have a left hand
side (lhs) and a right hand side (rhs).  First the interpreter evaluates
the rhs and after that it assigns the returned value to the lhs.

So if you write
   a      =   a + 1
   ^          ^
   |          |  
   lhs        rhs

the interprter first evaluates a+1, stores that value somewhere and
assigns then a to that value.  From now on the old value of a is
forgotten.

> ----------------------
> B. WHY WON'T THIS RUN?
> -----------------------
> #while 1 == 1: would perpetuate the printing of "help..."
> #l3 - l6 [extracted from another program] should limit it to five times
> count = 0
> max_count = 5
> while count > max_count:
>       count = count + 1
>       print "help, i'm caught in a loop"

> #but the program won't run

A while loop runs as long as the test in its head returns True.  Now
look at your test condition: what does it return?

> -----------------------------
> C.HAVE I PARSED THIS RIGHT?
> -----------------------------
> #values into/out of a pail;	#this is l1
> a = 1                   #this means a = non-zero, that it is;
> s = 0			#empty pail
> print "enter numbers to add to the sum"	#plus or minus
> print "enter 0 to quit"			#end the game
> while a != 0:		#i.e., you're still playing the game;
>       print "current sum:", s  #so we start at 0
>       a = input("nmber? ")     #and we alter s by a, superceding a = 1
>       s = s + a		       #and a is the increment
> 			       #reducing s to 0 won't close program
> print "total sum = ", s  #not indented cause it only runs at the end

> #l2, l3 provide the contents and the pail;

Right.

> #l5, l6 provide for a way to end the game;

IMO no. line 5 prints only a message to the screen. line 6 is the line
which holds the test condition.

> #playing the game is l4, plus the indented lines l7 - l9;

Maybe.  I wouldn't know which line should not belong to the game (except
line 1 since it's only a comment).  If you deleted any other line the
game wouldn't run the way it does now.


   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list