[Tutor] finding factorials

Gregor Lingl glingl@aon.at
Tue Jul 1 15:11:02 2003


Payal Rathod schrieb:

Hello Payal! I think I should have made some comments on your comments --

>On Mon, Jun 30, 2003 at 10:33:36AM -0700, Danny Yoo wrote:
>  
>
>>Same mistake as last time.  *grin* The code is simply dropping the value
>>that you're calculating from doing euclid().  We need to capture that
>>value:
>>    
>>
>
>Sorry Danny I am still not getting it. Now I am so confused that I think
>I don't know what exactly a function does and what is it it returns.
>
>I have posted my code below with some comments regarding issues I don't
>get.
>
>#!/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?
>
#- this will return the gcd of b and the rest when a is divided by b

>        else:
>                return a
>
>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. 
>
#-- I tried it out and it - not surprisingly - worked!

>
>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.
>  
>
#-- These remain 100 and 20 respectively, because your code doesn't contain
#-- statements, which change the values of x and y.
#--  Only the values of a and b (and c) are changed, but these are 
entirely different
#-- variables, local to the function euclid and not globally visible. 
The change of
#-- their values is revealed by your print-statements.

>print result
>
># The result is given as 20 which I think is wrong. 
>
#-- Why do you think this is wrong? What do you think is the greatest
#-- common divisor of 100 and 20?

># Is there anything 
># wrong with my mathematical logic or my function?
>  
>
#-- Hmm ??? See my previous posting.

Best wishes, Gregor

>With warm regards,
>-Payal
>
>
>  
>