Danny Yoo | 1 Jul 2003 22:46
Picon

Re: finding factorials


On Tue, 1 Jul 2003, Payal Rathod wrote:
>
> I have posted my code below with some comments regarding issues I don't
> get.

Hi Payal,

Sure, no problem.  Let's take a look:

> #!/usr/local/bin/python
>
> def euclid(a,b):
>         while b != 0:
>                 c = a
>                 a = b
>                 b = c % a
>                 print 'A = ', a
>                 print 'B = ', b
>                 return euclid(a,b)
> # What will this return exactly?
>         else:
>                 return a

I'll skip your first question for a moment.  We'll try to come back to it
later.

> x = 100
> y = 20
>
> result = euclid(x, y)
>
> # Why do we define x and y here seperately?
> # Can't we have result = euclid(100,20)
> # This apparently is not working.

Hmmm!  You're right: that should work too!  There was no need to do:

###
x = 100
y = 20
###

but I thought that your original code had done the same, so I didn't want
to change it.

Can you show us what happens when you try doing euclid(100, 20)?  It
should work.

> print x
> print y
>
> # This always prints 100 and 20. I think it should print the present
> # values of x and y which are not 100 and 20 always.

Let's try another experiment for a moment.  Take a look:

###
>>> def square(x):
...     return x * x
...
>>> x = 3
>>> y = 4
>>> z = square(x) + square(y)
###

At this point, what do you expect to see if we check up on the value of x,
y, and z?

(Your question touches on the idea of "local" scope.  We can talk about it
a little later when you feel more comfortable about functions.)

What happens if we do something like this:

###
>>> x = 42
>>> y = 42
>>> x = x + 1
###

Check with the interactive interpreter to see what the values of 'x' and
'y' are after we do those statement, just to make sure they're consistent
with what you expect.

The conceptual problem you might be running into is seeing '=' and
thinking it means math equality.  If so, be careful: it's not!

If this is what you're running into when you see something like:

    x = 42

then you need to think of it more like

    x <---------------- 42

That is, imagine a big honking arrow that pushes a value into a name,
squeezing out the old value in the process.  The technical term for this
is "assignment".  Some folks are too dignified to use the big honking
arrow, and will write it out on paper like this:

    x <- 42

And some computer languages do allow this kind of notation, which more
clearly shows that what we're doing isn't symmetric at all: we're pushing
stuff from the right hand side into the left hand side.  Unfortunately,
Python only uses the '=' notation to assign values to variable names, so
you have to use '=', even though it might look visually disturbing.

I don't know if this is the assignment issue is the thing that you're
getting caught on, but if so, I hope the explanation helps a little.

> print result
>
> # The result is given as 20 which I think is wrong. Is there anything
> # wrong with my mathematical logic or my function?

Math logic.  *grin*

The GCD of 100 and 20 is supposed to be 20. GCD(a,b) is the greatest
number that will divide both 'a' and 'b' evenly, and 20 fits that criteria
perfectly.

Hope this helps!

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor


Gmane