[Tutor] first steps
peter hodgson
py at humnet.ucla.edu
Sun Mar 28 03:25:37 EST 2004
hello; i've done my first forty or so tutorial programs; please take a
look at and tell me if i'm on track;
A. I DON'T UNDERSTAND THIS:
Here is another example of variable usage:
a = 1
print a
a = a + 1
print a
a = a * 2
print a
And of course here is the output:
1
2
4
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?].
----------------------
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
-----------------------------
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;
#l5, l6 provide for a way to end the game;
#playing the game is l4, plus the indented lines l7 - l9;
More information about the Tutor
mailing list