[Tutor] finding factorials - hmm..., gcd
glingl
glingl@mail.rg16.asn-wien.ac.at
Tue Jul 1 05:16:03 2003
--- Payal Rathod <payal-python@staticky.com> schrieb:
>
> 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?
> 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.
>
> 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.
>
> print result
>
> # The result is given as 20 which I think is wrong. Is there anything
> # wrong with my mathematical logic or my function?
>
> With warm regards,
> -Payal
>
Your code is ok and gives the right result!
e.g change: x, y = 25, 15 and you will get:
>>>
A = 15
B = 10
A = 10
B = 5
A = 5
B = 0
25
15
5
hmmm..., o.k., it's not perfektly ok, because ...
def euclid(a,b):
while b != 0:
c = a
a = b
b = c % a
# (1) normally it's not considered good practice to pu
# print-statements in a function which aims at calculating
# a numerical rsult. -
# But here let's use it for getting insight in the working
# of euclid
print 'A = ', a
print 'B = ', b
# (2) here you use an uncoditional return - so the body of
# the while-loop will be exited after the first run.
# Consequently it will never work as a loop and you could
# (a) use an if instead of while or
# (b) drop the return and recursive call of euclid -
# recursion also accomplishes some sort of iteration
# and should/need/(must?) not be done in addition
# to the while-iteration
return euclid(a,b)
else:
return a
x = 25
y = 15
result = euclid(x, y)
print x
print y
print result
################################
# If you decide for the use of while you arrive at:
def euclid(a,b):
while b != 0:
c = a
a = b
b = c % a
print 'A = ', a
print 'B = ', b
# return euclid(a,b)
# else:
return a
x = 25
y = 15
result = euclid(x, y)
print x
print y
print result
########################################
an alternative is to decide for the use of recursion,
in which case you arrive at:
def euclid(a,b):
if b != 0:
c = a
a = b
b = c % a
print 'A = ', a
print 'B = ', b
return euclid(a,b)
else:
return a
x = 198
y = 94
result = euclid(x, y)
print x
print y
print result
# (3) here you can leave out the three statements
#
# c = a
# a = b
# b = c % a
#
# if think carefully about which values are to be
# inserted as arguments in the recursive function
# call.
# What are the values of a and b respectively after
# execution of these 3 statements. (Remember: ist is
# perfectly right to insert arithmetic expressions like
# a / b, a+b etc as arguments - in which case they
# will be computed (evaluated( before the results are
# "bound" to the parameters.)
Finally you should delete (or comment out) the
print statements!
Hmmm, will this help?
Regards, Gregor
P.S.: Maybe http://www.ibiblio.org/obp/thinkCSpy/ ,
especially chapters 4 to 6,
contains material, which could be helpful for you.