<HTML><FONT FACE=arial,helvetica><FONT SIZE=3 FAMILY="SANSSERIF" FACE="Arial" LANG="0"><BR>
Johnathan H said,<BR>
<BR>
<BLOCKQUOTE TYPE=CITE style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px"></FONT><FONT COLOR="#000000" style="BACKGROUND-COLOR: #ffffff" SIZE=2 FAMILY="SANSSERIF" FACE="Arial" LANG="0">Can anyone help explain them to me?? <BR>
<BR>
This example gives the factorial of a number <BR>
<BR>
def fact(n):<BR>
r=1<BR>
while n >0:<BR>
r=r*n<BR>
n=n-1<BR>
return r <BR>
</BLOCKQUOTE><BR>
</FONT><FONT COLOR="#000000" style="BACKGROUND-COLOR: #ffffff" SIZE=3 FAMILY="SANSSERIF" FACE="Arial" LANG="0">line code # Literally spoken <BR>
</FONT><FONT COLOR="#000000" style="BACKGROUND-COLOR: #ffffff" SIZE=2 FAMILY="SANSSERIF" FACE="Arial" LANG="0">1) def fact(n): # define a function called fact and let me be<BR>
# able to set one of the variables called "n"<BR>
2) r = 1 # this variable "r" is set to 1 every time we call fact(n) into play <BR>
<BR>
3) while n>0: # so long as this statement is true, "n greater than 0" ,<BR>
# do the indented lines next, otherwise pass the indented lines <BR>
4) r=r*n # make my new "r" value equal to my old "r" value times <BR>
#the value of "n", then<BR>
5) n=n-1 # make my new "n" value equal to my old "n" value minus 1<BR>
# then check the while statement again since this is the last <BR>
# indented line of the while block, <BR>
# if it's still true then do this block again<BR>
<BR>
6) return r # we reach this statement when n is no longer greater than 0 <BR>
# n must be equal or less than 0<BR>
# so the value that is currently in r gets "return"ed <BR>
# in this case, since you are in the interpreter window, <BR>
# this will print the value of r after the function call fact(n)<BR>
>>> fact(5)<BR>
120<BR>
<BR>
which is 5*4*3*2*1 = 120<BR>
<BR>
If we call fact(5), then the first time thru the while loop r = 1 and n = 5<BR>
since n(5) is greater than 0, we get to go to line 4) <BR>
this is where r picks up it's first new value. r = r * n is really 5 = 1 * 5<BR>
But you say how can r have two values? This is not the math I learned in school!<BR>
This statement is really the result of a great short cut for convienience.<BR>
It stems from something like,<BR>
c = b + a<BR>
b = c <BR>
...but please don't think like this!<BR>
r = r * n will make so much sense to you after a while that I ashamed to show you<BR>
that last bit. <BR>
You should try to follow the fact(n) code all the way through, now.<BR>
This will give you great insite as to how the power(x,y=2) works.<BR>
What I will say about that one is that you can call it lots of ways.<BR>
Try power(2), then power(2,3), then power(2,4).... and so on for a hint.<BR>
The "y=2" part is a default value for "y". This means that if you don't give the<BR>
function a "y" value it will always use a 2. <BR>
<BR>
<BR>
Good Luck. Johnathan<BR>
I hope you enjoy programming, Python, and the good folks of the Tutor list.<BR>
Ray St. Marie<BR>
Rastm2@<removethistoreachme>users.sourceforge.net </FONT></HTML>