Re: Adding through recursion

2005-11-18 Thread [EMAIL PROTECTED]
Ben Finney wrote: > [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > def add(x, y): > > if x == 0: > > print y > > return y > > else: > > x -= 1 > > y += 1 > > add(x, y) > > To add to the other good advice in this thread: > > This is just one of many

Re: Adding through recursion

2005-11-18 Thread Donn Cave
In article <[EMAIL PROTECTED]>, Ben Finney <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > def add(x, y): > > if x == 0: > > print y > > return y > > else: > > x -= 1 > > y += 1 > > add(x, y) ... > def recursive_add

Re: Adding through recursion

2005-11-18 Thread Micah Elliott
On Nov 19, Ben Finney wrote: ... > This is just one of many reasons why I advocate always having a > *single* return statement, at the *end* of the function. Agreed that it's a good *general* practice, but sometimes early exit is useful and clear. This is somewhat of a religious topic. A goo

Re: Adding through recursion

2005-11-18 Thread Ben Finney
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > def add(x, y): > if x == 0: > print y > return y > else: > x -= 1 > y += 1 > add(x, y) To add to the other good advice in this thread: This is just one of many reasons why I advocate always having a *si

Re: Adding through recursion

2005-11-18 Thread Rocco Moretti
[EMAIL PROTECTED] wrote: > There is problaly a really simple answer to this, but why does this > function print the correct result but return "None": > > def add(x, y): > if x == 0: > print y > return y > else: > x -= 1 > y += 1 > add(x, y) > > prin

Re: Adding through recursion

2005-11-18 Thread Tim Williams (gmail)
On 18 Nov 2005 06:30:58 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED] > wrote:> I still don't get it. I tried to test with x = 0 and found that to Informative print statements can help you to see and understand  the program flow, especially if the problem is conceptual,  with no *real*  errors in yo

Re: Adding through recursion

2005-11-18 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > I still don't get it. I tried to test with x = 0 and found that to > work. How come since the value of y is right and it is printed right it > "turns into" None when returned by the return statement ? because you're not returning the value to the print statement; you're

Re: Adding through recursion

2005-11-18 Thread Gerard Flanagan
[EMAIL PROTECTED] wrote: > I still don't get it. I tried to test with x = 0 and found that to > work. How come since the value of y is right and it is printed right it > "turns into" None when returned by the return statement ? Martin, -a function should either return something or not. Your f

Re: Adding through recursion

2005-11-18 Thread Simon Brunning
On 18 Nov 2005 06:30:58 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I still don't get it. I tried to test with x = 0 and found that to > work. How come since the value of y is right and it is printed right it > "turns into" None when returned by the return statement ? There is no return

Re: Adding through recursion

2005-11-18 Thread [EMAIL PROTECTED]
I still don't get it. I tried to test with x = 0 and found that to work. How come since the value of y is right and it is printed right it "turns into" None when returned by the return statement ? -- http://mail.python.org/mailman/listinfo/python-list

Re: Adding through recursion

2005-11-18 Thread Simon Brunning
On 18 Nov 2005 05:22:47 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > There is problaly a really simple answer to this, but why does this > function print the correct result but return "None": > > def add(x, y): > if x == 0: > print y > return y > else: > x -

Re: Adding through recursion

2005-11-18 Thread Peter Tillotson
basically the other two points :-) you create a string of add functions add(2,4)--add(1,5)--add(0,6) only in the last ( add(0,6) ) explicitly returns y, in the else of add(1,5) you ignor it. If you want the starting add to return something sensible you need to find a way to pass it back up the

Re: Adding through recursion

2005-11-18 Thread Mark Jackson
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > There is problaly a really simple answer to this, but why does this > function print the correct result but return "None": > > def add(x, y): > if x == 0: > print y > return y > else: > x -= 1 > y += 1 >

Re: Adding through recursion

2005-11-18 Thread [EMAIL PROTECTED]
any change you want to have : "return add(x,y)" in the else ? [EMAIL PROTECTED] wrote: > There is problaly a really simple answer to this, but why does this > function print the correct result but return "None": > > def add(x, y): > if x == 0: > print y > return y > else:

Adding through recursion

2005-11-18 Thread [EMAIL PROTECTED]
There is problaly a really simple answer to this, but why does this function print the correct result but return "None": def add(x, y): if x == 0: print y return y else: x -= 1 y += 1 add(x, y) print add(2, 4) result: 6 None Martin -- http://mai